import Axios from 'axios';
import {getToken} from '@/utils/auth';
export const downloadFile = (url, params) => {
Axios.defaults.baseURL = '/';
Axios.get(url, {
headers: {
'Authorization': getToken()
},
params: {
id: params.id
},
responseType: "blob"
}).then(function (response) {
dataHandle(response);
}).catch(function (error) {
console.log(error);
});
}
function dataHandle(response) {
const fileName = response.headers['content-disposition'].match(
/filename=(.*)/
)[1]
const blob = new Blob([response.data]);
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, decodeURI(fileName));
} else {
const blobURL = window.URL.createObjectURL(blob);
const tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', decodeURI(fileName));
if (typeof tempLink.download === 'undefined') {
tempLink.setAttribute('target', '_blank');
}
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
}