美文网首页
Vue从Restful API下载文件并保存

Vue从Restful API下载文件并保存

作者: 老中医167788 | 来源:发表于2022-08-09 11:58 被阅读0次

    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)
    })
    

    相关文章

      网友评论

          本文标题:Vue从Restful API下载文件并保存

          本文链接:https://www.haomeiwen.com/subject/weknwrtx.html