2012-11-08 40 views
2

我使用this plugin(只是基本版本),並利用所有整潔的功能進行更新以使用我自己的UI(使用Knockout.js和Twitter Bootstrap) 。下面是一些代碼片段爲背景:JQuery文件上傳插件,Internet Explorer和IFrame傳輸

// The file is sent to an ASP.NET MVC Web Api service to do all the business logic/DB stuff 
    uploadUrl = http://web.api.url/?apikey=key 

    $("#fileUpload" + "@index").fileupload({ 
     headers: { 
      'Authorization': "@Html.AccessToken()", 
      'Accept': $.support.ajax ? "application/json" : "text/plain" 
     }, 
     url: uploadUrl, 
     add: function (e, data) { 
      $.each(data.files, function (index, file) { 
       // add to KO viewmodel 
      }); 
      data.submit(); 
     }, 
     fail: function (e, data) { 
      var error = data.errorThrown; 
      var text = data.textStatus; 
     }, 
     done: function (e, data) { 
      // do some more viewmodel operations 
     }, 
     progress: function (e, data) { 
      var progressPercentage = parseInt(data.loaded/data.total * 100, 10); 
      // update viewmodel 
     } 
    }); 

#fileUpload<Index>元素是文件輸入

而這在Chrome,FF和Safari但(驚喜驚喜)的偉大工程,沒有在IE中。當我試圖從我的文件輸入中選擇一個文件時,我收到了一個非常奇怪的迴應 - 瀏覽器打開一個下載對話框?

Do you want to open or save ?apikey=key (61 bytes) from webapiserver? 

我嘗試使用IE瀏覽器的腳本調試器與我的文件上傳事件監聽器內斷點,它甚至從來沒有使得它的內部。我在我的研究中看到過各種帖子和文章,指出應用程序/ json的接受類型會搞砸IE,所以我在我的代碼中有一個條件來嘗試處理它。

有什麼我失蹤?

+0

你有沒有得到解決這個的實現? –

回答

5

你的問題在於服務器端。

IE是有點笨,他接受服務器提供的第一個內容類型,所以你總是需要記住首先嚐試用text/plaintext/html回答。

然而這被提及in the FAQ from the jQuery plugin

0

這是因爲返回的數據是application/json。您需要服務器指定內容類型的text/html,以防止IE將其解釋爲下載。

1

這裏是C#中的修復程序爲我工作

var result = Json(statuses, JsonRequestBehavior.AllowGet); 

    if (HttpContext.Request.AcceptTypes.Contains("application/json")) 
    { 
     result.ContentType = "application/json"; 
    } 
    else // Hack for IE9 
    { 
     result.ContentType = "text/plain"; 
    } 

    return result;