admin管理员组

文章数量:1535258

主要思路

详细的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;

本文标签: 浏览器文件无弹窗