AjaxUpload在IE返回信息提示下载

本文介绍了一个使用Spring Boot的REST控制器进行EXCEL文件上传并返回JSON响应的示例,详细展示了如何避免IE浏览器自动下载问题,以及前端如何解析JSON响应。

最近在做一个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();
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值