将二进制流文件转换为可下载文件类型

58 阅读1分钟
封装接口(记得加上responseType: "blob"export function downloadMineFile(data) {
      return axios({ url: '/message/historicalData/outputReport', method: 'post',data, responseType: "blob" })
   }
downLoadFile() {
        let list = { 
            endTime: "2023-07-21 11:20:02",
            startTime: "2023-07-21 11:16:48"
        }
        downloadMineFile(list).then(res => {
            // Blob 对象表示一个不可变、原始数据的类文件对象(File 接口都是基于Blob)
            // 有些时候取的值可能是res.data,这个根据自己的项目需求
            const blob = new Blob([res], {type: 'application/octet-stream'})
            let url = window.URL.createObjectURL(blob)
            var el = document.createElement("a")
            document.body.appendChild(el)
            el.href = url
            // 要下载的文件名
            el.download =`生产报表${list.entityName}.xls`
            this.fileName = el.download
            el.click()
        }).catch(err => {
            this.$message({
                type: 'error',
                message: "导出失败!"
            })
        })
    },