I currently have the following:
@page @model pcore31.AddimgModel @{ Layout = null; } <!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><title>Addimg</title></head><body><h4>Petimg</h4><hr /><div class="row"><div class="col-md-4"><form enctype="multipart/form-data" method="post"><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="Petimg.PetName" class="control-label"></label><input asp-for="Petimg.PetName" class="form-control" /><span asp-validation-for="Petimg.PetName" class="text-danger"></span></div><div class="form-group"><label asp-for="Upload1" class="control-label"></label><input asp-for="Upload1" class="form-control" type="file" /></div><div class="form-group"><label asp-for="Upload2" class="control-label"></label><input asp-for="Upload2" class="form-control" type="file" /></div><div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div></form></div></div><div><a asp-page="Index">Back to List</a></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }</body></html>
And code behind:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using pcore31.Models; using pcore31.Helpers; using System.IO; namespace pcore31 { public class AddimgModel : PageModel { private readonly pcore31.Models.DatabaseContext _context; private IWebHostEnvironment _environment; private string fileName; //private string filename; public AddimgModel(pcore31.Models.DatabaseContext context, IWebHostEnvironment environment) { _context = context; _environment = environment; } public IActionResult OnGet() { return Page(); } [BindProperty] public Petimg Petimg { get; set; } [BindProperty] public IFormFile Upload1 { get; set; } [BindProperty] public IFormFile Upload2 { get; set; } // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return Page(); } int k = -1; int j = 0; string[] items = new string[2]; string[] newfile = new string[2]; Lqtest csql = new Lqtest(); var idcount = csql.Idcount(); string stringcount = idcount.ToString(); foreach (IFormFile postpic in Request.Form.Files) { k = k + 1; j = j + 1; string sj = j.ToString(); if (postpic == null) { items[k] = ""; newfile[k] = ""; } else { items[k] = postpic.Name.ToString(); int thisid = idcount + j; string thiscount = thisid.ToString(); var filename = $"{postpic.FileName}"; Array getparts = StrHelper.SplitString(filename); string parta = getparts.GetValue(0).ToString(); string partb = getparts.GetValue(1).ToString(); newfile[k] = parta + thiscount + "." + partb; var file = Path.Combine(_environment.ContentRootPath, "wwwroot\\images\\upload", newfile[k]); using (var fileStream = new FileStream(file, FileMode.Create)) { await postpic.CopyToAsync(fileStream); } } } Petimg.PetName = StrHelper.Ucfirst(Petimg.PetName); Petimg.Petpic1 = $"{newfile[0]}"; Petimg.Petpic2 = $"{newfile[1]}"; _context.Petimg.Add(Petimg); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } } }
This all works.
My question is how do I convert this to an array, like:
@page @model pcore31.AddimgModel @{ Layout = null; } <!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><title>Addimg</title></head><body><h4>Petimg</h4><hr /><div class="row"><div class="col-md-4"><form enctype="multipart/form-data" method="post"><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="Petimg.PetName" class="control-label"></label><input asp-for="Petimg.PetName" class="form-control" /><span asp-validation-for="Petimg.PetName" class="text-danger"></span></div><div class="form-group"><input asp-for="Upload[]" class="form-control" type="file" /></div><div class="form-group"><input asp-for="Upload[]" class="form-control" type="file" /></div><div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div></form></div></div><div><a asp-page="Index">Back to List</a></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }</body></html>
How do I change the binding to reflect an array of files:
// Currently I have: [BindProperty] public Petimg Petimg { get; set; } [BindProperty] public IFormFile Upload1 { get; set; } [BindProperty] public IFormFile Upload2 { get; set; } // And what would this change to: foreach (IFormFile postpic in Request.Form.Files) ???
Thanks
And I am not wanting
type="file" multiple
I want the array, as files can come from more than one folder.