【问题标题】:Upload File in MVC and create a link to Download the the file在 MVC 中上传文件并创建一个链接以下载文件
【发布时间】:2014-03-06 18:44:14
【问题描述】:

我已经为上传文件创建了这个代码。但它不会在我创建的 App_Data/Uploads 文件夹中上传任何文件。 这是代码>>

In view>>

<form action="~/Views/Home/_SaveUpdate" 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 this my Handler>>

[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(HttpContext.Server.MapPath("~/App_Data/Uploads"), fileName);
                    file.SaveAs(path);
                }
            }
            return RedirectToAction("Index");
        }

请告诉我还需要做什么。另外,如何生成下载文件的链接。

【问题讨论】:

    标签: asp.net-mvc http user-interface file-upload


    【解决方案1】:

    首先,表单的操作现在指向~/Views/Home/_SaveUpdate,而根据您的操作后应该是/Home/Index。 其次,确保您在 App_Data 文件夹中创建了 Upload 文件夹。 这应该可以解决上传问题。

    查看:

    <form action="/Home/Index" 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>
    

    如果要显示所有上传文件的下载链接,则应将图像存储在与 App_Data 不同的文件夹中。由于安全原因,不能直接访问 App_Data 文件夹。

    可以在here找到一个显示目录中文件的好例子

    【讨论】:

      【解决方案2】:

      我使用了您的代码,并对其进行了一些更改以对其进行测试。它确实有效。

      对于我的测试,我只是更改了操作的名称。 你确定你的表单 Action 正确吗?

      <form action="~/Views/Home/_SaveUpdate" 
      

      因为它与您的处理程序的名称不匹配:

      public ActionResult Index(IEnumerable ....
      

      我的测试: 确保您的上传文件夹存在,否则您会遇到异常。 处理程序:

              [HttpPost]
          public ActionResult FileUploadPost(IEnumerable<HttpPostedFileBase> files)
          {
              foreach (var file in files)
              {
                  if (file.ContentLength > 0)
                  {
                      var fileName = Path.GetFileName(file.FileName);
                      var path = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), fileName);
                      file.SaveAs(path);
                  }
              }
              return RedirectToAction("Index");
          }
      

      观点:

       <form action="FileUploadPost" method="post" enctype="multipart/form-data">
      
              <label for="file1">Filename1:</label>
              <input type="file" name="files" id="file1" />
      
              <label for="file2">Filename2:</label>
              <input type="file" name="files" id="file2" />
      
              <input type="submit" />
          </form>
      

      【讨论】:

        猜你喜欢
        • 2020-06-27
        • 2012-01-26
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        相关资源
        最近更新 更多