C# 从 jQuery 中选择多个文件后的 ASP.Net 上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/363286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:36:18  来源:igfitidea点击:

ASP.Net Upload of multiple files after choosing them from jQuery

c#asp.netjqueryfile-uploadmultifile-uploader

提问by Steve Davies

I have used a jQuery multiple file upload control [ MultiFile from fyneworks http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Overview] to collect some filenames but can't work out how to upload them on the server.

我使用了 jQuery 多文件上传控件 [来自 fyneworks http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Overview 的MultiFile ] 来收集一些文件名,但无法弄清楚如何上传它们在服务器上。

The standard asp:FileUpload control only seems to allow single files and I don't want to use the swfupload control, just plain old aspx.

标准的 asp:FileUpload 控件似乎只允许单个文件,我不想使用 swfupload 控件,只是简单的旧 aspx。

回答by Steve Davies

(I have answered this question myself, I just had problems finding the answer via goole or SO and it seems useful ...)

(我自己已经回答了这个问题,我只是在通过 goole 或 SO 找到答案时遇到了问题,这似乎很有用...)

This code works for what I need, thanks to Suprotim Agarwal http://www.dotnetcurry.com/ShowArticle.aspx?ID=68

此代码适用于我需要的东西,感谢 Suprotim Agarwal http://www.dotnetcurry.com/ShowArticle.aspx?ID=68

Once the files have been chosen using a suitable jQuery multiple upload control (eg MultiFile from fyneworks http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Overview) and the submit button has been clicked, call the following code in the aspx file

一旦使用合适的 jQuery 多重上传控件(例如来自 fyneworks http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Overview 的MultiFile )选择了文件并且点击了提交按钮,调用aspx文件中的以下代码

HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
    HttpPostedFile hpf = hfc[i];
    if (hpf.ContentLength > 0)
    {               
        hpf.SaveAs(Server.MapPath("Uploads") + "\" + System.IO.Path.GetFileName(hpf.FileName));
    }
}   

回答by soe

HttpFileCollection uploads = HttpContext.Current.Request.Files;

HttpFileCollection 上传 = HttpContext.Current.Request.Files;

for (int i = 0; i < uploads.Count; i++) {

for (int i = 0; i < uploads.Count; i++) {

        HttpPostedFile upload = (HttpPostedFile)uploads[i];