直接下载后台传回的文件内容

贴上代码

// 下载文件
const content = res
const blob = new Blob([content])
const fileName = 'export_' + this.exportRow.id + '.txt'
if ('download' in document.createElement('a')) { // 非IE下载
  const elink = document.createElement('a')
  elink.download = fileName
  elink.style.display = 'none'
  elink.href = URL.createObjectURL(blob)
  document.body.appendChild(elink)
  elink.click()
  URL.revokeObjectURL(elink.href) // 释放URL 对象
  document.body.removeChild(elink)
} else { // IE10+下载
  // navigator.msSaveBlob(blob, fileName)
}

说明:

objectURL = URL.createObjectURL(object);

object用于创建 URL 的 File 对象、Blob 对象或者 MediaSource 对象。​

在每次调用 createObjectURL() 方法时,都会创建一个新的 URL 对象,即使你已经用相同的对象作为参数创建过。当不再需要这些 URL 对象时,每个对象必须通过调用 URL.revokeObjectURL() 方法来释放。

 

参考链接:https://blog.csdn.net/lanseguhui/article/details/107655757

相关文章: