ajax上传文件,(IE浏览器new FormData的兼容问题)

问题记录:2018-07-30

ajax方法上传文件时用到 new FormData()方法来传递数据,但在IE8浏览器下提示错误。改用ajaxSubmit方式进行提交,引用到jquery.form.js插件

<%-- 原方法 

var formData = new FormData($("#uploadForm")[0]);
$.ajax({ url: "<%=mainWeb%>/goods/count/importExcel.do",
data: formData,
cache: false,
async: true,
type: "post",

..........

--%>

 

<%-- ajaxSubmit --%>

表单:

<form id="uploadForm" action="" method="post" enctype="multipart/form-data" style="text-align:left;" target="uploadScanFileIfrm">
<input type="file" name="uploadFile" id="uploadFile" style="height: 27px; width: 98%;" />
</form>

按钮:

<button id="btn_upload" onclick="javascript:doUploadFile()">开始导入</button>

js(引用jquery.form.js插件):

function doUploadFile(){
$("#uploadForm").ajaxSubmit({
type: 'post',
url: "<%=mainWeb%>/goods/count/importExcel.do",
success: function (response) {
var message = JSON.parse(response);
if(message.result){
alert("导入成功");
parent.doQuery();
}else{
layer.alert(message.msg);
}
},
error: function (response) {
var message = JSON.parse(response);
alert("导入失败:"+message.msg);
}
});

}

IE下ajax返回出现下载提示,在spring-mvc配置文件中添加

<bean id="mappingJacksonHttpMessageConverter"

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

posted @ 2018-07-30 14:42  小小娃娃  阅读(1434)  评论(0编辑  收藏  举报