vue-axios 下载 zip文件包

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
=> {

            })

相关文章