后台返回流,前端blob下载文件

476 阅读1分钟

image.png

downLoadFile(){
        let param='入库申请单模板'
        this.$http.get('/entryApplication/downloadFile?name='+param,{responseType:'blob'}).then(res=>{
          //res.data var blob = new Blob([content]);

          const blob = new Blob([res.data]);//new Blob([res])中不加data就会返回下图中[objece objece]内容(少取一层)
          const fileName = '导出入库申请模板.xlsx';//下载文件名称
          if (window.navigator.msSaveBlob) {
            console.log('===')
            try {
              window.navigator.msSaveBlob(blob, fileName);
            } catch (e) {
              console.log(e);
            }
          } else {
            console.log('-----')
            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);
          }
        })
      }