axios下载文件取不到content-disposition

axios 提交表单同时下载文件

  • 问题描述:
    直接访问url下载文件时是有Content-Dispositionheader的,使用axios时取不到该header。
  • 后台代码
 	@GetMapping("/exportConfig")
    public void exportConfig(@RequestParam("projectId") Integer projectId, HttpServletResponse response) throws Exception {
        logger.debug("access schedule exportConfig , projectId={}",projectId);
        String configStr=jobService.exportConfig(projectId);
        JobGroup jobGroup=jobService.getJobGroupByProjectId(projectId);
        String name = new String((jobGroup.getEnvName()+"_"+jobGroup.getProjectName()+".schedule").getBytes("utf-8"), "ISO-8859-1");
        response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + name);
        OutputStream output = response.getOutputStream();
        output.write(configStr.getBytes());
    }
  • 解决办法
//响应时在响应头里添加 Access-Control-Expose-Headers 
 response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");

更多推荐

axios下载文件取不到content-disposition