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