Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.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
Javascript 通过jQuery AJAX上传文件后,HttpPostedFile.ContentLength始终为-2_Javascript_Jquery_Asp.net_Ajax_Ashx - Fatal编程技术网

Javascript 通过jQuery AJAX上传文件后,HttpPostedFile.ContentLength始终为-2

Javascript 通过jQuery AJAX上传文件后,HttpPostedFile.ContentLength始终为-2,javascript,jquery,asp.net,ajax,ashx,Javascript,Jquery,Asp.net,Ajax,Ashx,我对jQueryAjax和ASHX行为有奇怪的问题。这是我的密码: <input type="file" ID="FileUpload1" multiple="multiple" /> <input type="button" ID="Button1" Text="Upload Selected File(s)" /> public void ProcessRequest(HttpContext上下文) { context.Response.ContentType

我对jQueryAjax和ASHX行为有奇怪的问题。这是我的密码:

<input type="file"  ID="FileUpload1"  multiple="multiple" />
<input type="button" ID="Button1"  Text="Upload Selected File(s)" />
public void ProcessRequest(HttpContext上下文)
{
context.Response.ContentType=“text/plain”;
尝试
{
字符串dirFullPath=HttpContext.Current.Server.MapPath(“~/MediaUploader/”);
字符串[]文件;
整数文件;
files=System.IO.Directory.GetFiles(dirFullPath);
numFiles=files.Length;
numFiles=numFiles+1;
字符串str_image=“”;
foreach(context.Request.Files中的字符串s)
{
HttpPostedFile file=context.Request.Files[s];
字符串文件名=file.fileName;
字符串fileExtension=file.ContentType;
如果(!string.IsNullOrEmpty(文件名))
{
fileExtension=System.IO.Path.GetExtension(文件名);
str_image=“MyPHOTO”+numFiles.ToString()+文件扩展名;
字符串pathToSave_100=HttpContext.Current.Server.MapPath(“~/files/”)+str_image;
文件.SaveAs(路径为save_100);
}
}
//此处的数据库记录更新逻辑()
context.Response.Write(str_图像);
}
捕获(异常ac)
{
}
}
一切似乎都很好,但结果是:

context.Request.Files[0]
{System.Web.HttpPostedFile}
    ContentLength: -2
    ContentType: "image/png"
    FileName: "icon-large.png"
    InputStream: {System.Web.HttpInputStream}
我可以接收文件名,但
ContentLength
始终为
-2
,保存文件后,其大小仅为
0
字节。你能帮我解决这个问题吗

更新: 我发现了一些新的东西,它与ASP.net Development Server配合使用(在Visual Studio中按F5键直接运行应用程序)很好,但IIS 8.5配置有问题

我的web.config请求长度参数还有:

    <httpRuntime requestValidationMode="2.0" maxRequestLength="10240000" requestLengthDiskThreshold="10240000" />

更新2:
将应用程序池的托管管道模式更改为经典模式可以解决问题,但我将丢失URL重写,因此无法更改管道模式。有什么想法吗?

我找到了解决这个问题的办法。我不知道这是好是坏,但解决了我的案子:

我曾经

void Application_BeginRequest(object sender, EventArgs e) 

global.asax

文件来管理请求,因为内容在那里正确可见。
还有其他想法吗?

尝试在:jQuery.each($())和$.ajax({})之间打印数据这是我的请求:POST HTTP/1.1连接:保持活动内容长度:338630 Pragma:no-cache-cache-Control:no-cache-Accept:/Origin:X-Request-With:XMLHttpRequest-Content-Type:multipart/form-data;boundary=--WebKitFormBoundaryMg4rlBFcHS4txlHr引用:接受编码:gzip,deflate接受语言:en-US,en;q=0.8,fa;q=0.6------WebKitFormBoundaryMg4rlBFcHS4txlHr内容配置:表单数据;name=“file-0”;filename=“icon large.png”内容类型:image/png并且我的内容长度是338630。我使用fiddler检查我的请求它可能会有所帮助似乎这不是解决方案,但在将contentType更改为“json”后,context.Request.ContentLength为338630,但context.Request.Files为空:{System.Web.HttpFileCollection}base{System.Collections.Specialized.NameObjectCollectionBase}:{System.Web.HttpFileCollection}所有键:{string[0]}