2016-07-07 78 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的

文件管理請求,因爲內容在那裏正確可見。 任何其他想法?