运用: 下载excel时,后台设置了excel标题,要去请求头去取,传输过程中文会有乱码的情况,需要编码下。 let fileDownload = require("js-file-download"); fileDownload(res.data,decodeURIComponent(res.headers['content-disposition'].split("=")[1]));...
apiDownLoad是axios封装了一个get请求,重点配置responseType:'blob',解决文件内容乱码。 后台使用Springboot,JAVA代码如下: @GetMapping("/downloadFile")publicResponseEntity<FileSystemResource>downloadFile(@RequestParam(name="fileName")StringfileName)throwsException{//获得完整的路径Stringpath="D://share/"+fileName...
你的问题是关于Django后端处理文件下载时返回乱码数据,并且文件可以下载但无法打开。首先,这个问题可能是因为你的Django后端在返回文件时,Content-Type没有设置正确。对于非文本文件(如docx、xlsx等),应该使用application/octet-stream作为Content-Type。你的代码中已经设置了这一点,所以问题可能出在其他地方。 另一个可能...
先让后端自己下载下来看看是不是乱码的,没准是后端的问题。。。
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); ...
文件下载失败或无法打开:检查后端服务是否正确返回了文件的二进制数据,并且响应头中设置了正确的Content-Type和Content-Disposition。 文件名乱码:确保后端服务在设置Content-Disposition响应头时,编码文件名为UTF-8。 下载链接无效:检查前端代码中生成下载链接的逻辑是否正确,确保生成的链接是有效的URL。
let fileName= res.headers['content-disposition'].match(/fushun(\S*)xls/)[0]; fileDownload(res.data,fileName); //如果用方法一 ,这里需要安装 npm installjs-file-download --save ,然后引用 var fileDownload = require('js-file-download'),使用详情见github; ...
})} 下载插件 npm install js-file-download -S 运⽤:下载excel时,后台设置了excel标题,要去请求头去取,传输过程中⽂会有乱码的情况,需要编码下。let fileDownload = require("js-file-download");fileDownload(res.data,decodeURIComponent(res.headers['content-disposition'].split("=")[1]));补充...
let filename = decodeURI(contentDisposition.split('fileName=')[1] || contentDisposition.split('filename=')[1]); // 注意这里的 result.data ,如果只传 result 的话,最后下载出来的excel文件,里面显示的是 [object Object] let blob = new Blob([result.data],{type: result.headers['content-type...
{type:"application/vnd.ms-excel"});constfileName=res.headers["content-disposition"].match(/filename=(\S*).xls/)[1];constelink=document.createElement("a");elink.download=JSON.parse(fileName)+".xls";elink.href=window.URL.createObjectURL(blob);elink.click();window.URL.revokeObjectURL(e...