Vue从Restful API下载文件并保存

2022-08-09  本文已影响0人  老中医167788

downloadService.js

import http from '../http'

export function PackageDestKongStorageExportToExcel(params) {
    return http({
        method: 'get',
        url: '/api/station/PackageDestKongStorage/ExportToExcel',
        params:params,
        responseType: 'blob'  // 重要
    })
}

example:

download(fileData){
          const url = window.URL.createObjectURL(new Blob([fileData],{type: 'application/octet-stream'}))
          const link = document.createElement('a')
          link.href = url
          link.setAttribute('download', '下载文件名称.xls') // 下载文件的名称及文件类型后缀
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); // 下载完成移除元素
          window.URL.revokeObjectURL(url); // 释放掉blob对象
},

PackageDestKongStorageExportToExcel(params).then(res => {
            this.download(res.data)
})
上一篇下一篇

猜你喜欢

热点阅读