使用 new FormData 上传文件导致IE不兼容的问题
程序员文章站
2022-07-07 17:00:00
...
好吧,直奔主题!
多数浏览器都支持new FormData($( "#fileUploadForm")[0]),但是IE10下却显示
FormData未定义
//AJAX文件上传
$.ajax({
url: path + "/fileManager/uploadFile",
type: 'post',
data: new FormData($( "#fileUploadForm")[0]),
async: true,
cache: false,
contentType: false, // 告诉jQuery不要去处理发送的数据
processData: false, // 告诉jQuery不要去设置Content-Type请求头
success: function (data) {
mini.hideMessageBox(messageid);
mini.confirm(data.msg ,"提示",function delConfirm(action){
window.CloseOwnerWindow();
});
},
error: function (data) {
mini.hideMessageBox(messageid);
var msg = data.msg;
if(null == msg || "" == msg || "undefined" == msg){
msg = "操作出现异常";
}
mini.confirm(msg ,"提示",function delConfirm(action){
window.CloseOwnerWindow();
});
}
});
}
怎么办呢?只有换一个提交策略了ajaxSubmit
$("#batchAdd").ajaxSubmit({
type: 'post',
url: path+'/sysInfo/importSysInfo',
success: function (data) {
var msssage = JSON.parse(data);
mini.alert(msssage.msg ,"提示",function ImportSysConfirm(action){
window.CloseOwnerWindow();
});
},
error: function (data) {
var msssage = JSON.parse(data);
var msg = msssage.msg;
if(null == msg || "" == msg || "undefined" == msg){
msg = "操作出现异常";
}
mini.alert(msg,"提示",function ImportSysConfirm(action){
window.CloseOwnerWindow();
});
}
});
不过这里的时候也出现了一个问题,由于返回的时候JSON,IE8总是要提示是否下载JSON文件
好办取配置文件里配一下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 处理responseBody 里面日期类型 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<!-- 为null字段时不显示 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
大功告成
多数浏览器都支持new FormData($( "#fileUploadForm")[0]),但是IE10下却显示
FormData未定义
//AJAX文件上传
$.ajax({
url: path + "/fileManager/uploadFile",
type: 'post',
data: new FormData($( "#fileUploadForm")[0]),
async: true,
cache: false,
contentType: false, // 告诉jQuery不要去处理发送的数据
processData: false, // 告诉jQuery不要去设置Content-Type请求头
success: function (data) {
mini.hideMessageBox(messageid);
mini.confirm(data.msg ,"提示",function delConfirm(action){
window.CloseOwnerWindow();
});
},
error: function (data) {
mini.hideMessageBox(messageid);
var msg = data.msg;
if(null == msg || "" == msg || "undefined" == msg){
msg = "操作出现异常";
}
mini.confirm(msg ,"提示",function delConfirm(action){
window.CloseOwnerWindow();
});
}
});
}
怎么办呢?只有换一个提交策略了ajaxSubmit
$("#batchAdd").ajaxSubmit({
type: 'post',
url: path+'/sysInfo/importSysInfo',
success: function (data) {
var msssage = JSON.parse(data);
mini.alert(msssage.msg ,"提示",function ImportSysConfirm(action){
window.CloseOwnerWindow();
});
},
error: function (data) {
var msssage = JSON.parse(data);
var msg = msssage.msg;
if(null == msg || "" == msg || "undefined" == msg){
msg = "操作出现异常";
}
mini.alert(msg,"提示",function ImportSysConfirm(action){
window.CloseOwnerWindow();
});
}
});
不过这里的时候也出现了一个问题,由于返回的时候JSON,IE8总是要提示是否下载JSON文件
好办取配置文件里配一下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value><!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 处理responseBody 里面日期类型 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<!-- 为null字段时不显示 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
大功告成