vue下载zip文件提示损坏

vue下载zip文件提示损坏

问题代码

前端

export function exportCheckFile() {
  return request({
    url: '/exportCheckFile',
    method: 'get'
  })
}

后端

   public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file, boolean deleteOnExit) throws IOException {
        response.setCharacterEncoding(request.getCharacterEncoding());
        response.setContentType("application/octet-stream");
        FileInputStream fis =  new FileInputStream(file);
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        IOUtils.copy(fis, response.getOutputStream());
        response.flushBuffer();
        fis.close();
        if (deleteOnExit) {
            file.deleteOnExit();
        }
    }

整改代码

添加两句

responseType: 'blob',
headers:{ 'Content-Type': 'application/json; application/octet-stream'},
export function exportCheckFile() {
  return request({
    url: '/exportCheckFile',
    method: 'get',
    responseType: 'blob',
    headers:{ 'Content-Type': 'application/json; application/octet-stream'},
  })
}

你可能感兴趣的:(vue,vue.js,java,前端)