YanTianFeng的知识库

Want Coding

Want Reading

文章 89

访问 18443

评论 2

头像

YanTianFeng

发私信

文章 89
访问 18443
评论 2
Technology and Code
返回顶部

Knowledge  下载文件流

标签   文件流  

  ( 14 )       ( 0 )


下载文件流代码实现

function downFile(res, fileName) {
 const blob = new Blob([res], {
   type: `application/pdf;charset=UTF-8`
 });

 if (window.navigator.msSaveOrOpenBlob) {
   // 兼容ie11
   window.navigator.msSaveOrOpenBlob(blob, fileName);
 } else {
   const url = URL.createObjectURL(blob);
   const downloadElement = document.createElement("a");
   downloadElement.href = url;
   downloadElement.download = fileName;
   document.body.appendChild(downloadElement);
   downloadElement.click();
   downloadElement.remove();
   URL.revokeObjectURL(url);
 }
}
try {
 this.$http.post("/api/company/bill/download", {
   id,
 }, {
   responseType: 'blob'
 }).then((res) => {
   this.showLoading = false;
   downFile(res, `123.pdf`)
   return;
 });
} catch (error) {
 console.error('error: ', error);
}

重点在于 responseType : 'blob' ,指定已 blob 形式接收