2016-07-07 76 views
3

我对jQuery AJAX和ASHX行为有奇怪的问题。这里是我的代码:通过jQuery上传文件后,HttpPostedFile.ContentLength始终为-2 AJAX

<input type="file" ID="FileUpload1" multiple="multiple" /> 
<input type="button" ID="Button1" Text="Upload Selected File(s)" /> 
function Upload() { 
    var data = new FormData(); 
    jQuery.each($('#FileUpload1')[0].files, function (i, file) { 
     data.append('file-' + i, file); 
    }); 

    $.ajax({ 
     url: "/basic/fileupload/FileUploadHandler.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); 
     } 
    }); 
} 
public void ProcessRequest(HttpContext context) 
{ 
    context.Response.ContentType = "text/plain"; 
    try 
    { 
     string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/"); 
     string[] files; 
     int numFiles; 
     files = System.IO.Directory.GetFiles(dirFullPath); 
     numFiles = files.Length; 
     numFiles = numFiles + 1; 
     string str_image = ""; 

     foreach (string s in context.Request.Files) 
     { 
      HttpPostedFile file = context.Request.Files[s]; 
      string fileName = file.FileName; 
      string fileExtension = file.ContentType; 

      if (!string.IsNullOrEmpty(fileName)) 
      { 
       fileExtension = System.IO.Path.GetExtension(fileName); 
       str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension; 
       string pathToSave_100 = HttpContext.Current.Server.MapPath("~/files/") + str_image; 
       file.SaveAs(pathToSave_100); 
      } 
     } 
     // database record update logic here () 

     context.Response.Write(str_image); 
    } 
    catch (Exception ac) 
    { 
    } 
} 

Eeverything似乎要被罚款,但结果是:

context.Request.Files[0] 
{System.Web.HttpPostedFile} 
    ContentLength: -2 
    ContentType: "image/png" 
    FileName: "icon-large.png" 
    InputStream: {System.Web.HttpInputStream} 

我可以接收文件名,但ContentLength总是-2并保存后,该文件只是0字节是大小。你能帮我解决这个问题吗?

UPDATE: 我发现一些新的东西,它的正常工作与ASP.net开发服务器(通过推动在Visual Studio F5键可直接运行的应用程序),但东西是错误的IIS 8.5的配置

也我的web.config请求长度参数为:

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

更新2: 改变应用程序池对托管管道模式到经典就能解决问题,但我会失去我的URL Rewritin克,所以我不能改变我的管道模式。任何想法?

+0

已尝试打印jQuery.each($())和$ .ajax({}) – Destrif

+0

之间的数据以下是我的请求:POST http://localhost/FileUploadHandler.ashx HTTP/1.1 Connection:keep-活着 的Content-Length:338630 杂注:无缓存 缓存控制:无缓存 接受:*/* 产地:HTTP://本地主机 X-要求 - 由于:XMLHttpRequest的 内容类型:多/形式数据; border = ---- WebKitFormBoundaryMg4rlBFcHS4txlHr Referer:http://localhost/car-information.html Accept-Encoding:gzip,deflate Accept-Language:zh-CN,en; q = 0.8,fa; q = 0.6 ------ WebKitFormBoundaryMg4rlBFcHS4txlHr Content-Disposition:form-data; NAME = “文件-0”; filename =“icon-large.png” Content-Type:image/png –

+0

而且我的内容长度是338630。我用小提琴来检查我的请求 –

回答

0

我发现解决方案来解决这个问题。我不知道是不是罚款或没有,但解决了我的情况:

我用

void Application_BeginRequest(object sender, EventArgs e) 

Global.asax的

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