vue blob格式文件流下载
vue 下载文件
首先reques(config)里面设置responseType: ‘blob’;
然后:对返回的流文件处理,获取返回头里面的文件名,
let aa = res.headers['content-disponsition'];
let bb = decodeURIComponent(dlNameDisposition.split("filename=")[1]) || "";
downloadByData (文件流数据,文件名称)
最后写上文件下载方法,如下:downloadByData (文件流数据,文件名称)
export function downloadByData ( data, filename, mime, type="application/octet-stream", bom ) {
const blobData = typeof bom !== "undefined" ? [ bom, data] : [data];
const blob = new Blob(blobData, {type});
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, filename);
} else {
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute("download", filename);
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute("target", '_blank');
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
}