前端自定义下载文件流到本地

当前端收到后端传过来的文件流时,前端手动下载文件到本地,方法如下:

1、发起请求时,要设置相应类型为arrayBuffer:

axios.get(url,{responseType: 'arrayBuffer'})

2、手动下载文件流到本地:

let url = window.URL.createObjectURL(new Blob([res.body],{type: 'application/octet-stream'}));   // 如果不加type,当后端传回的文件名不加后缀的话,浏览器会默认加上.txt后缀,改变文件类型
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', res.response.headers['content-disposition'].split('=')[1]);  // 设置下载文件名称
link.click();
link.remove();

 

posted @ 2023-02-28 09:21  抽风的皮鞭  阅读(160)  评论(0)    收藏  举报