前端通过流下载文件

// file
export function downloadFile(data, name, suffix) {
  if (!name) {
    name = parseTime(new Date()) + '-' + name + '.xlsx';
  }
  // 对于IE浏览器特殊处理
  if (window.navigator && window.navigator.msSaveBlob) {
    window.navigator.msSaveBlob(new Blob([data]),name + (suffix ? ('.' + suffix) : ''));
  } else {
    const url = window.URL.createObjectURL(new Blob([data]));
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    const fileName = name + (suffix ? ('.' + suffix) : '');
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
}
// excel
export function downloadExcel(data, fileName,suffix) {
  let fix = suffix==undefined?'.xls':suffix
  fileName = parseTime(new Date()) + '-' + fileName;
  if (window.navigator && window.navigator.msSaveBlob) {
    window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + fix)
  } else {
    let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
    let link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', fileName + fix)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link); //下载完成移除元素
    window.URL.revokeObjectURL(url); //释放掉blob对象
  }
}
上一篇:文献阅读(57)ICML Workshop2020-Deep Graph Contrastive Representation Learning


下一篇:11个简单的Java性能调优技巧,傻瓜都能学会!