import axios from 'axios';
function downloadData(source, fileName, downloadType) {
if (downloadType === 'blob') {
const blobData = new Blob([source], { type: source.type || 'application/octet-stream' });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blobData, fileName);
} else {
const url = window.URL.createObjectURL(blobData);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}
} else if (downloadType === 'url') {
axios({
url: source,
method: 'GET',
responseType: 'blob'
})
.then(response => {
const blobData = new Blob([response.data], { type: response.headers['content-type'] || 'application/octet-stream' });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blobData, fileName);
} else {
const url = window.URL.createObjectURL(blobData);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
}
})
.catch(error => {
console.error('下载出错:', error);
});
}
}
export default downloadData;