1、在springmvc中,用ajaxfileupload来异步上传文件,
首先不能用@responseBody返回json,可用response.getWrite().write(json)返回数据
public void doUpload(@RequestParam("myingf") MultipartFile file,HttpServletRequest request,
HttpServletResponse response)
2、增加
response.setContentType("text/html;chartset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(JSON.toJSONString(example));
注:response.setCharacterEncoding(“UTF-8”);,不加的话返回中文会乱码
3、前台接收参数
$.ajaxFileUpload({
url: "${ctx}/aaa/doUpload.do",// 用于文件上传的服务器端请求地址
secureuri: false,// 一般设置为false
fileElementId: fileId,// 文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: "text", // 返回值类型 一般设置为json
params: {
"receiverIdentity": receiverIdentity
},
success: function(data) {
var jsonData = JSON.parse(data);
// 正面
$("#receiverIdentityFrontPath",formId).val(jsonData.receiverIdentityFrontPath);
$("#frontPathNameF",formId).val(jsonData.frontPathNameF);
},
error: function(data,status,e) {
alert(e);
}
});
原文链接:https://www.f2er.com/ajax/161673.html