2

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
  • use jquery file upload control its more simple Commented Jan 26, 2016 at 15:59

2 Answers 2

2

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"> 
Sign up to request clarification or add additional context in comments.

4 Comments

I do have that on my form @using (Html.BeginForm("InsertDevelopment", "Developments", FormMethod.Post, new { enctype = "multipart/form-data" }))
And HttpContext.Current.Request.Files is empty?
Yes @bri . HttpContext.Current.Request.Files is empty
Thanks guys the problem was from the names of my file inputs i now receive values from my view to the controller. @bri +1 for a detailed response. Thanks again
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.