前端下载二进制文件

前端下载二进制文件

downloadFile =function(data, fileName){
  if (!data) {
    return
  }
  let url = window.URL.createObjectURL(new Blob([data]))
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', fileName)
  document.body.appendChild(link)
  link.click();
  link.remove();
 };
  
 # 使用
 fileName = 'demo' + formatDate(new Date(),'yyyyMMdd')  + '.txt'
 downloadFile(res, fileName);

相关推荐