IE8/9 JQuery.Ajax 上傳文件無效


IE8/9 JQuery.Ajax 上傳文件有兩個限制:

  1. 使用 JQuery.Ajax 無法上傳文件(因為無法使用 FormData,FormData 是 HTML5 的一個特性,IE8/9 不支持)
  2. 使用 JQuery Form 上傳,contentType 只能為 text/html,因為如果是 application/json 類型,IE8/9 會以文件下載的方式展現 json 數據。

所以,在 IE8/9 中使用 JQuery 上傳只能使用 Form 的方式,示例代碼:

$("#" + formid).ajaxSubmit({
    type: "post",
    url: '/Upload/UploadImage',
    data: { fileName: fileName },
    cache: false,
    dataType: 'json',
    success: function (data) {
        console.log(data.success);
        console.log(data.message);
    }
});

ASP.NET MVC 后台代碼:

[HttpPost]
public ActionResult UploadImage(string fileName)
{
    var image = Request.Files[fileName];
    var uploadResult = { success = true, message = “” };
    return Json(uploadResult, "text/html", Encoding.Unicode, JsonRequestBehavior.AllowGet);  //重點這段代碼
}

參考資料:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM