码迷,mamicode.com
首页 > 移动开发 > 详细

vue-axios 下载 zip文件包

时间:2020-02-20 13:17:49      阅读:934      评论:0      收藏:0      [点我收藏+]

标签:app   position   元素   ken   body   blob   -o   split   filename   

axios({
                method: ‘get‘,
                url: ‘/api/group/export‘,
                params: data,
                headers: {
                    Authentication: localStorage.getItem(‘token‘),
                    Accept: ‘application/vnd.openxmlformats-officedocument‘
                },
                responseType: ‘blob‘
            }).then(response => {
                console.log(response.data);
                
                const blob = new Blob([response.data], {type: ‘application/zip‘});
                const filename = response.headers[‘content-disposition‘];
                const downloadElement = document.createElement(‘a‘);
                const href = window.URL.createObjectURL(blob); //创建下载的链接
                console.log(href);
                
                downloadElement.href = href;
                [downloadElement.download] = [filename.split(‘=‘)[1]];
                document.body.appendChild(downloadElement);
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                window.URL.revokeObjectURL(href); //释放blob对象
            }).catch((error) => {

            })

  

axios({
                method: ‘get‘,
                url: ‘/api/group/export‘,
                params: data,
                headers: {
                    Authentication: localStorage.getItem(‘token‘),
                    Accept: ‘application/vnd.openxmlformats-officedocument‘
                },
                responseType: ‘blob‘
            }).then(response => {
                console.log(response.data);
                
                const blob = new Blob([response.data], {type: ‘application/zip‘});
                const filename = response.headers[‘content-disposition‘];
                const downloadElement = document.createElement(‘a‘);
                const href = window.URL.createObjectURL(blob); //创建下载的链接
                console.log(href);
                
                downloadElement.href = href;
                [downloadElement.download] = [filename.split(‘=‘)[1]];
                document.body.appendChild(downloadElement);
                downloadElement.click(); //点击下载
                document.body.removeChild(downloadElement); //下载完成移除元素
                window.URL.revokeObjectURL(href); //释放blob对象
            }).catch((error=> {

            })

vue-axios 下载 zip文件包

标签:app   position   元素   ken   body   blob   -o   split   filename   

原文地址:https://www.cnblogs.com/baifubin/p/12335089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!