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); //重点这段代码
}

参考资料:

上一篇:vue路由跳转报错解决


下一篇:IE8/9 本地预览上传图片