js 根据链接下载 excel 文件
function downloadWithBlob(url) {
const elt = document.createElement('a');
elt.setAttribute('href', url);
elt.setAttribute('download', 'file.png');
elt.style.display = 'none';
document.body.appendChild(elt);
elt.click();
js 根据文件流下载文件
const fileDownload = (content, name = 'fileName', suffix = 'xls') => {
// 添加字节序标识,避免乱码
// const data = `\uFEFF${content}`;
const blob = new Blob([content], { type: 'application/vnd.ms-excel,charset=UTF-8' });
const downloadElement = document.createElement('a');
// 创建下载链接
const href = window.URL.createObjectURL(blob);
downloadElement.href = href;
// 下载文件名
downloadElement.download = `${name}.${suffix}`;
document.body.appendChild(downloadElement);
downloadElement.click();
// 移除元素
document.body.removeChild(downloadElement);
// 释放blob对象
window.URL.revokeObjectURL(href);
};
调用
fileDownload(url, ‘文件’ + (new Date()).valueOf());