You can create a new utils/downloadFile.js in the project and import it when using it;
Download 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)
}
}
Download 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);
}
Download 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);
}
The specific and more detailed type type corresponding to the Blob can be moved here:
https://www.iana.org/assignments/media-types/media-types.xhtml