可在项目中新建utils/downloadFile.js,使用时引入即可;
下载CSV
export function downloadCsv(fileName,data){
const url = getUrl(data,{});
cosnt a = document.createElement('a');
a.href = url;
a.download = `${fileName}.csv`;
a.click();
window.URL.revokeObjectURL(url);
function getUrl(encoded,options){
const dataBlob = new Blob([`\ufeff${encoded}`],{type:"text/plain;charset=utf-8"})
return window.URL.createObjectURl(dataBlob)
}
}
下载Excel
export function downloadExcel(fileName,data){
const a = document.createElement('a');
let dataBlob = new Blob([data],{type:"application/x-excel"});
a.style.display = 'none';
a.href = URL.createObjectURl(dataBlob);
a.download = `${fileName}.xls`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
下载Zip
export function downloadzip(fileName,data){
const a = document.createElement('a');
let dataBlob = new Blob([data],{type:"application/zip"});
a.style.display = 'none';
a.href = URL.createObjectURl(dataBlob);
a.download = `${fileName}.zip`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
具体更详细的Blob对应的type类型可移步于此:
https://www.iana.org/assignments/media-types/media-types.xhtml