解決“Resource interpreted as Document but transferred with MIME type application/json”問題

來源:互聯網
上載者:User

標籤:儲存   其他   oca   ie瀏覽器   length   color   測試   ide   ati   

在上傳圖片時,使用ajax提交,返回的資料格式為json。在測試時發現IE瀏覽器中,上傳圖片後,沒有顯示圖片,而是彈出一個提示:是否儲存UploadImg.json檔案;而在其他瀏覽器中正常。

在Chrome中調試後發現,圖片上傳成功後,瀏覽器給出了一個警告:Resource interpreted as Document but transferred with MIME type application/json。

原來後台代碼在返回json資料時,響應資料的ContentType預設為“application/json”,IE瀏覽器的新版本(IE10、IE11)會把該類型解釋為檔案下載操作。

後台代碼:

public JsonResult UploadImgToLocal()        {            var FileMaxSize = 10240; //檔案大小,單位為K                 var model = new ImgUploadResult();            try            {                HttpPostedFile myFile = HttpContext.Current.Request.Files[0];                if (myFile != null)                {                    string fileExtension = myFile.FileName.Substring(myFile.FileName.LastIndexOf(‘.‘));                    if (!CheckValidExt(fileExtension))                    {                        return Json(new { UploadCode = 104, massege = "檔案格式錯誤" });                    }                    else                    {                        if (myFile.ContentLength > FileMaxSize * 1024)                        {                            return Json(new { UploadCode = 105, massege = "檔案過大" });                        }                        else                        {                                //上傳                                //返回結果                                return Json(new { UploadCode = 100, massege = "", sourceUrl = model.SourceImgUrl, bigUrl = model.BigImgUrl, thumbUrl = model.ThumbImgUrl });                                #endregion                            }                            catch                            {                                return Json(new { UploadCode = 101, massege = "上傳失敗" });                            }                        }                    }                }                else                {                    return Json(new { UploadCode = 102, massege = "請上傳檔案" });                }            }            catch            {                return Json(new { UploadCode = 101, massege = "上傳失敗" });            }        }

將代碼中的

return Json(new { UploadCode = 100, massege = "", sourceUrl = model.SourceImgUrl, bigUrl = model.BigImgUrl, thumbUrl = model.ThumbImgUrl });

改為:

JsonResult json = new JsonResult();json.ContentType = "text/html";json.Data = new { UploadCode = 100, massege = "", sourceUrl = model.SourceImgUrl, bigUrl = model.BigImgUrl, thumbUrl = model.ThumbImgUrl };return json;

修改響應資料的ContentType類型後,返回的資料類型為json字串,這樣就會相容IE瀏覽器了。

解決“Resource interpreted as Document but transferred with MIME type application/json”問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.