jquery - 使用 ASP.NET MVC 和 jQuery 多文件上传插件进行多文件上传

标签 jquery asp.net asp.net-mvc file-upload multifile-uploader

我正在使用 jQuery 多文件上传插件来上传多张图片。但表单仅发布 1 个顶部项目。 fiddler (POST):

POST /Images/UploadImages HTTP/1.1
Host: localhost:4793
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:4793/images
Cookie: .ASPXAUTH=EFAC4E03FA49056788028048AE1B099D3EB6D1D61AFB8E830C117297471D5689EC52EF40C7FE2CEF98FF6B7C8CAD3AB741A5E78F447AB361A2BDD501331A88C7B75120611CEA4FECA40D972BB9401472
Content-Type: multipart/form-data; boundary=---------------------------1509898581730
Content-Length: 290022

-----------------------------1509898581730
Content-Disposition: form-data; name="album"

1
-----------------------------1509898581730
Content-Disposition: form-data; name="file[]"; filename="Blue hills.jpg"
Content-Type: image/jpeg

...

这是我的代码:

<% using (Html.BeginForm("UploadImages", "Images", FormMethod.Post, new { enctype = "multipart/form-data"}))
       {%>

    <%= Html.DropDownList("album", (IEnumerable<SelectListItem>)ViewData["Albums"])%>
      <br />    
    <input type="file" name="file[]" id="file" class="multi" accept="jpg|png" />
      <br />
     <input type="submit" name="submit" value="Submit" />

    <% } %>

Controller 的代码:

public ActionResult UploadImages(FormCollection formValue)
    {           
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                continue;

            //do something with file
        }
        return RedirectToAction("Index");
    }

请告诉我如何解决这个问题。也许您可以建议其他方式让用户上传多张图像。 TIA

PS。除了 sript 控件生成的 html 代码之外:

<input id="file" class="multi" type="file" accept="jpg|png" name="file[]" style="position: absolute; top: -3000px;"/>
<input id="file_F1" class="multi MultiFile" type="file" accept="jpg|png" name="file[]" style="position: absolute; top: -3000px;"/>
<input id="file_F2" class="multi MultiFile" type="file" accept="jpg|png" name="file[]" style="position: absolute; top: -3000px;"/>
<input id="file_F3" class="multi MultiFile" type="file" accept="jpg|png" name="file[]" style="position: absolute; top: -3000px;"/>
<input id="file_F4" class="multi MultiFile" type="file" accept="jpg|png" name="file[]"/>

最佳答案

我找到了解决方案。答案是将 HttpPostedFileBase 更改为 IEnumerable(如果您要上传多个文件)。

我遇到了和你一样的问题。这解决了我的问题。这也是一个很好的链接:Phil Haacks's post

关于jquery - 使用 ASP.NET MVC 和 jQuery 多文件上传插件进行多文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1992874/

相关文章:

javascript - 如果 html 站点中没有事件表单,则按 Enter 键发出警报

javascript - 选择图像时获取类中的所有 id

javascript - 如何计算用户在页面上活跃的时间?

jquery - Telerik MVC 扩展与 JqGrid 冲突?

asp.net-mvc - 在 AJAX 请求之前单击按钮的客户端验证

javascript - 在 fancytree 插件上选择/取消选择具有相同 ID 的所有节点

javascript - 国家代码后面的0怎么去掉

asp.net - Web 服务器上更多与更快的内核

c# - 在 aspx 页面中使用 Asp.Net WebForms 上的属性资源本地化文本

javascript - Web 文本编辑器中的 RTF 格式