Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在.ashx处理程序中上载多个文件_C#_Asp.net_Ajax_Handler_Ashx - Fatal编程技术网

C# 无法在.ashx处理程序中上载多个文件

C# 无法在.ashx处理程序中上载多个文件,c#,asp.net,ajax,handler,ashx,C#,Asp.net,Ajax,Handler,Ashx,System.InvalidCastException:无法强制转换类型为的对象 “System.String”以键入“System.Web.HttpPostedFile” 这是我的响应标题中显示的错误。 我无法通过ajax向处理程序发送多个文件 jQuery var data = new FormData(); jQuery.each($('#multipleFileUpload')[0].files, function (i, file) {

System.InvalidCastException:无法强制转换类型为的对象 “System.String”以键入“System.Web.HttpPostedFile”

这是我的响应标题中显示的错误。 我无法通过ajax向处理程序发送多个文件

jQuery

            var data = new FormData();
            jQuery.each($('#multipleFileUpload')[0].files, function (i, file) {
                data.append('file-' + i, file);
            });

            $.ajax({
                url: "../handlers/project/sell/galleryUpload.ashx",
                type: "POST",
                contentType: false,
                processData: false,
                cache: false,
                async: true,
                data: data,
                error: function (data) {
                    alert("Erro no envio de fotos do projecto. " + data.status);
                }
            });
处理程序:

foreach (HttpPostedFile file in context.Request.Files)
{ ... } 
//it gives error in this line

AjaxUpload依赖于浏览器,您使用的浏览器是什么

您的表单是否包含enctype=“多部分/表单数据”

看看这个答案:


我也有同样的问题。我不知道为什么,但在使用
foreach
循环迭代context.Request.Files集合时会出现问题

相反,使用传统的
for
循环,并显式转换为HttpPostedFile

HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count;i++ )
{
    HttpPostedFile file = files[i];
    string fname = context.Server.MapPath("~/uploads/" + file.FileName);
    file.SaveAs(fname);
}
HttpFileCollection files=context.Request.files;
对于(int i=0;i
资料来源:

我使用google chrome和所有其他浏览器,但我使用相同的上传方式,不会将错误返回到一个文件,但多个文件都有错误!要上载一个文件,您使用的代码与上面完全相同?