2012-05-31 123 views
1

我正在使用插件通過Ajax上傳圖像。這個插件的js源代碼是here請求標頭在IE中不可用

如果你看到從1200起線,你注意到這一點:

// build query string 
    params = params || {}; 
    params['qqfile'] = name; 
    var queryString = qq.obj2url(params, this._options.action); 

    xhr.open("POST", queryString, true); 
    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); 
    xhr.setRequestHeader("X-File-Name", encodeURIComponent(name)); 
    xhr.setRequestHeader("Content-Type", "application/octet-stream"); 
    xhr.send(file); 

所以基本上,上傳的文件名可以作爲qqfile參數或頁眉VAR:X-File-Name。這兩種方法在FF /鉻完美地工作與此ASP.NET MVC代碼:

public JsonResult AjaxUpload(String qqfile) 
    { 

     String fileName = System.Web.HttpContext.Current.Request.Headers["x-file-name"]; 
     //rest of the code 
    } 

但在IE中,沒有這些作品的?頭var爲空(在調試我清楚地看到,這個參數是未發送)和1qqfile始終爲"System.Web.HttpPostedFileWrapper"

有誰知道如何解決這個問題?

ps:這個插件的演示是here

回答

4

我終於能夠做到這一點,如:

public JsonResult AjaxUpload(HttpPostedFileWrapper qqfile) 
    { 
     //IE 
     if (qqfile != null) 
     { 
      fileName = qqfile.FileName; 
     } 
     else 
     { 
      fileName = System.Web.HttpContext.Current.Request.Headers["x-file-name"]; 
     } 
    //rest of the code 
    } 

似乎在IE HttpPostedFileWrapper包含文件名和其他屬性(而在其他的瀏覽器,這是null),很奇怪,我不得不這樣做的特例爲IE。

+0

幹得好先生:)認爲這將是一個頭痛。您的解決方案適用於我。唯一的事情是qqFile.FileName獲取我的完整路徑,所以我不得不拆分出實際的文件名。 –

+0

哇...它的工作,thanx! –