最近在做一个EXCEL上传返回信息时,遇到在IE返回信息提示下载。一搜都是在后台设置
response.setContentType("text/html;charset=utf-8");返回JSON串
然后在jq中用text接收把对应的JSON串转为JSON提取对应的信息,经操作发现不设置response也可以,具体代码如下;
@RestController
@RequestMapping("hs/easapi/tbeorderbill")
public class TbEorderbillController {
@Autowired
private TbEorderbillService tbEorderbillService;
/**
* 文件上传
*/
@RequestMapping("/fileupload")
@RequiresPermissions("hs:easapi:tbeorderbill:fileupload")
public String fileupload(@RequestParam("file") MultipartFile file /*, HttpServletResponse response*/){
String msg="";
File delFile=null;
// response.setContentType("text/html;charset=utf-8");
PrintWriter out = null;
JSONObject json=new JSONObject();
try {
File secondFile= new File(file.getOriginalFilename());
InputStream ins = file.getInputStream();
TbEorderbillUtils.inputStreamToFile(ins, secondFile);
msg=tbEorderbillService.orderManage(secondFile);
delFile = new File(secondFile.toURI());
json.put("msg",msg);
return json.toString();
}catch (Exception e){
e.printStackTrace();
}finally{
if(delFile!=null){
delFile.delete();
}
}
}
return json.toString();
}
}
jq如下:
new AjaxUpload('#upload', {
action: baseURL + "../hs/easapi/tbeorderbill/fileupload",
name: 'file',
autoSubmit:true,
responseType:"text",
onSubmit:function(file, extension){
if (!(extension && /^(xls|xlsx|xlsm)$/.test(extension.toLowerCase()))){
alert('只支持xls、xlsx、xlsm格式!');
return false;
}
$("#upload").attr('disabled','ture');
$('#showMsg').show();
},
onComplete : function(file, data){
$('#showMsg').hide();
$('#upload').removeAttr('disabled');
var repObj = JSON.parse(data);
alert(repObj.msg);
vm.reload();
}
});
本文介绍了一个使用Spring Boot的REST控制器进行EXCEL文件上传并返回JSON响应的示例,详细展示了如何避免IE浏览器自动下载问题,以及前端如何解析JSON响应。
1万+

被折叠的 条评论
为什么被折叠?



