Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
use jquery file upload control its more simple – Arunprasanth KV 2 days ago
up vote 2 down vote accepted

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"> 
share|improve this answer
    
I do have that on my form @using (Html.BeginForm("InsertDevelopment", "Developments", FormMethod.Post, new { enctype = "multipart/form-data" })) – uzeeOnCodes 2 days ago
    
And HttpContext.Current.Request.Files is empty? – bri 2 days ago
    
Yes @bri . HttpContext.Current.Request.Files is empty – uzeeOnCodes 2 days ago
    
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 – uzeeOnCodes 2 days ago

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.