问题
I am creating a web application for an estate agency and on one of the forms used to upload properties i need to upload multiple images or files like floorplans in pdf format to a folder and store their path in the database(SQL SERVER).
I have tried doing it on my own using some help from online resources but have not been able to sort it out.
This is the part of my view with the file upload.
<div class="file_inputs">
<input type="file" id="dev_brochure" name="dev_brochure" class="form-control adminarea_files">
<input type="file" id="devListingImg1" name="devListingImg1" class="form-control adminarea_files">
<input type="file" id="devListingImg2" name="devListingImg2" class="form-control adminarea_files">
<input type="file" id="devListingImg3" name="devListingImg3" class="form-control adminarea_files">
</div>
This is my controller that processes the file upload
[HttpPost]
public ActionResult InsertDevelopment(development dev, IEnumerable <HttpPostedFileBase> files)
{
try
{
var supportedFileTypes = new [] {"jpg", "jpeg", "png", "gif", "pdf"};
foreach(var file in files)
{
if(file.ContentLength > 0)
{
var fileExt = Path.GetExtension(file.FileName).Substring(1);
if(!supportedFileTypes.Contains(fileExt))
{
TempData["Msg"] = "You have tried to upload a file type that is not allowed";
return RedirectToAction("Create");
}
}
}
var devBrochure = Path.GetFileName(Request.Files["dev_brochure"].FileName);
var devListingPhoto1_LinkName = Path.GetFileName(Request.Files["devListingImg1"].FileName);
var devListingPhoto2_LinkName = Path.GetFileName(Request.Files["devListingImg2"].FileName);
var devListingPhoto3_LinkName = Path.GetFileName(Request.Files["devListingImg3"].FileName);
return View("Create");
}
catch(Exception e)
{
TempData["Msg"] = "Development upload failed :" + e.Message;
return View("Create");
}
}
My main problem here is that the files from the view are not being received in the controller action's IEnumerable <HttpPostedFileBase> files
parameter.
I would appreciate any form of guide to fix this. Thanks
回答1:
If you are just posting a form, then make sure the content type of the form is set to multipart/form-data.
<form action="yourAction" enctype="multipart/form-data" method="post">
If you're trying to post using jquery/ajax, then check out this excellent post on the topic. You basically need to get jquery to mimic a form post with that content type.
Using HttpFileCollectionValueProvider
If (based on your comment/clarification), that isn't your problem, then it could be how you are trying to upload those files at the controller level.
I've always followed this advice when uploading multiple files. In short, try ditching that HttpPostedFileBase parameter OR try matching up your file object name with your parameter name
If you want to stick with "files" as the parameter name:
<input type="file" name="files[0]" id="files_0" class="form-control adminarea_files">
<input type="file" name="files[1]" id="files_1" class="form-control adminarea_files">
<input type="file" name="files[2]" id="files_2" class="form-control adminarea_files">
<input type="file" name="files[3]" id="files_3" class="form-control adminarea_files">
回答2:
Make sure, your form tag specifies the attributes
enctype="multipart/form-data" method="post"
Also make sure that your input-Elements have their name Attributes set to
name="files[0]"
name="files[1]"
and so on
来源:https://stackoverflow.com/questions/35017713/upload-multiple-files-filepath-using-asp-net-mvc