在asp.net mvc中上传多个文档文件

时间:2016-03-24 07:01:32

标签: c# asp.net-mvc

我需要在文件上传控件的帮助下上传多个文档。我为Image类型做了同样的事情。它完美地工作,因为它只需要1个图像文件。但是多文件上传无效。什么是我的代码中的缺陷我该怎么做?

  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;
                    }
                }
            }
    }

1 个答案:

答案 0 :(得分:1)

请参阅此链接以上传多个文件, http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/

基本理念是我们可以简单地拥有 多个文件输入都具有相同的名称。

 <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>

和控制器

[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");
}