解决IE浏览器自动打开文件无弹窗保存问题

主要思路

详细的IE默认文档类型原理可以百度其他文章,如果解决此类问题:
修改请求头类型是关键,IE浏览器会识别文件类型进行弹窗下载(IE默认的好像是text类型就直接打开了)
例如:headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
采用SpringMVC框架当中的ResponseEntity<byte[]>实现文件下载。

    @RequestMapping("/downfile")
    public ResponseEntity<byte[]> download(String path,String fileName) throws IOException {

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"),"ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            System.out.println("========>"+e);
        }
        File file = new File(path);
            if(file.exists()){
                return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(
                        file), headers,HttpStatus.OK);
            }
            headers.setContentDispositionFormData("attachment", "error.txt");
            return new ResponseEntity<byte[]>("发送错误.".getBytes(), headers,
                    HttpStatus.OK);
        }

注意:如果出现项目路径错误问题可以采用转发的方式返回给前端,前端再进行拼接项目地址。

String url1 = "/downfile?path="+result.getAddress()+"&fileName="+filesaveByReportId.getFilenames();
window.location.href = "../analysis" + data.address;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值