首页 > 其他 > 详细

vue 下载带有token校验的文件

时间:2020-11-28 14:24:44      阅读:177      评论:0      收藏:0      [点我收藏+]

正常下载代码

window.location.href = baseURL + "/api/storage/download?fileName=" + encodeURI(fileName) + "&fileUrl=" + fileUrl;

window.location.href不能带请求头,添加参数

带token代码

download(row.id).then(response => {

        const { data, headers } = response;
        const fileName = headers[‘content-disposition‘].replace(/\w+;filename=(.*)/, ‘$1‘);
        let blob = new Blob([data], {type: headers[‘content-type‘]});
        // let objectURL = window.URL.createObjectURL(blob);
        // window.location.href = objectURL;
        console.log(fileName);

        let dom = document.createElement(‘a‘)
        let url = window.URL.createObjectURL(blob)
        dom.href = url
        // dom.download = decodeURI(fileName)
        dom.download = decodeURI(row.name+".xls");
        dom.style.display = ‘none‘
        document.body.appendChild(dom)
        dom.click()
        dom.parentNode.removeChild(dom)
        window.URL.revokeObjectURL(url)
      });

其他方案为尝试

用axios或者你用的请求库,配置完header后,get请求到文件,然后用file-saver这个包把拿到的文件保存下来就好了

参考资料

vue 下载带有token校验的文件

原文:https://www.cnblogs.com/wlh1995/p/14052161.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!