1

I have a requirement of uploading the multiple documents with the help of file upload control. The same thing I had done for the Image type. It is working perfectly since it takes only 1 image file. But the multiple file upload is not working. What's the flaw in my code How can I do it?

  Html Code:
  ----------
   @using (Html.BeginForm("Index", "Block", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
       <div class="col-lg-4" id="FileUp">
           <label>Upload File</label>
           @Html.TextBoxFor(m=>m.DocumentFilesPath, new { @name="fileUpload", @id="fileUpload", @type = "file",  @multiple="multiple", @class="form-control dropDownLabel", @onchange="fileTypeCheck()" } )
       </div>

   // What should I specify the type for DocumentFilesPath in @Html.TextBoxFor(m=>m.DocumentFilesPath) in my strongly typed model? 

       <div class="col-lg-8>                                 
          <button type="button" class="btn btn-primary" id="SaveBlock" onclick="checkSubmit()">
          <span class="glyphicon glyphicon-floppy-save"></span>Save
              </button>
       </div>
    }

  My Controller Action:
  ---------------------

    [Authorize]
    [HttpPost]
    public ActionResult Index(BlockViewModel model)
    {
        if (model.BlockID == 0)
        {
            HttpPostedFileBase file = Request.Files["imgUpload"] as HttpPostedFileBase;    // I am able to get the single file here
            if (file.ContentLength > 0)
            {
                file.SaveAs(Server.MapPath("~/Images/uploads/") + file.FileName);

                model.ImageFilepath = Server.MapPath("~/Images/uploads/") + file.FileName;
            }

            IEnumerable<HttpPostedFileBase> docFiles = Request.Files["fileUpload"] as IEnumerable<HttpPostedFileBase>;   // But here docFiles is null
            //IEnumerable<HttpPostedFileBase> docFiles = fileUpload as IEnumerable<HttpPostedFileBase>;
            if (docFiles != null)
            {
                foreach (HttpPostedFileBase docFile in docFiles)
                {
                    if (docFile.ContentLength > 0)
                    {
                        docFile.SaveAs(Server.MapPath("~/Images/Files/") + docFile.FileName);

                        model.DocumentFilesPath = Server.MapPath("~/Images/Files/") + docFile.FileName;
                    }
                }
            }
    }
14
  • Why are you using Request.Files["imgUpload"]? Just add a parameter IEnumerable<HttpPostedFileBase> documentFilesPath to the method (and remove the pointless new { @name="fileUpload" } in your view which does nothing) Commented Mar 24, 2016 at 7:05
  • @StephenMuecke I had tried in that way too. I got the result as null Commented Mar 24, 2016 at 7:06
  • Then that suggests an issue with your plugin. What is it and how is it configured? And what is fileTypeCheck()? Commented Mar 24, 2016 at 7:08
  • @StephenMuecke Yes, it contains enctype = "multipart/form-data" Commented Mar 24, 2016 at 7:08
  • 1
    @REDEVI_, You dot need to have multiple file inputs all with the same name - you can have just one marked with the multiple="multiple" attribute (as OP is doing) Commented Mar 24, 2016 at 7:14

1 Answer 1

1

Refer this link for multiple file uploads ,, http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/

Basic Idea is We can simply have multiple file inputs all with the same name.

 <form action="" method="post" enctype="multipart/form-data">

      <label for="file1">Filename:</label>
      <input type="file" name="files" id="file1" />

      <label for="file2">Filename:</label>
      <input type="file" name="files" id="file2" />

      <input type="submit"  />
    </form>

and in Controller

[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
  foreach (var file in files) {
    if (file.ContentLength > 0) {
      var fileName = Path.GetFileName(file.FileName);
      var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
      file.SaveAs(path);
    }
  }
  return RedirectToAction("Index");
}
Sign up to request clarification or add additional context in comments.

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.