IE8/9 JQuery.Ajax 上傳文件有兩個限制:
- 使用 JQuery.Ajax 無法上傳文件(因為無法使用 FormData,FormData 是 HTML5 的一個特性,IE8/9 不支持)
- 使用 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); //重點這段代碼
}
參考資料: