js实现文件流下载文件
export function handleDownLoadFile(response, type, fileName) {
let blob = new Blob([response], {
type: type + ';charset=utf-8'
});
let src = window.URL.createObjectURL(blob);
if (src) {
let link = document.createElement('a');
link.style.display = 'none';
link.href = src;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(src);
}
}
export function handleDownLoadFileFromHeader(response) {
const names = response.headers['content-disposition'];
const str = names.replace(' ', '');
const arr = str.split(';');
let fileName = '';
arr.some(s => {
if (s.indexOf('filename') != -1) {
fileName = s.split('=')[1];
}
return s.indexOf('filename') != -1;
});
let src = window.URL.createObjectURL(response.data);
if (src) {
let link = document.createElement('a');
link.style.display = 'none';
link.href = src;
link.setAttribute('download', decodeURI(decodeURI(fileName)));
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(src);
}
}