vue 从文件流下载文件
downLoadQueryVehicleCSV('/tvdms/search/exportIllegalVehiclePassingAll.do',params).then(res =>this.fullscreenLoading = false).catch(res => {this.fullscreenLoading = false});function downLoadQuery
·
方法一:
downLoadQueryVehicleCSV(
'/tvdms/search/exportIllegalVehiclePassingAll.do',
params).then(res =>
this.fullscreenLoading = false
).catch(res => {
this.fullscreenLoading = false
});
function downLoadQueryVehicleCSV(url, params) {
return Axios({
headers: {
'Content-Type': 'application/json'
},
responseType: 'blob', //一定要写
method: 'post',
url: url,
data: params
}).then(res => {
const type = 'application/gzip';
const blob = new Blob([res.data], {type: type});
const name = decodeURI(
res.headers['content-disposition'].split('=')[1]);
if (window.navigator.msSaveBlob) { //没有此判断的话,ie11下的导出没有效果
window.navigator.msSaveBlob(blob,
name);
} else {
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob);
//后台再header中传文件名
downloadElement.href = href;
downloadElement.download = name;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);// 下载完成移除元素
}
return Promise.resolve(true);
}).catch(function (error) {
console.log(error);
let response = error.response;
if (response) {
let status = response.status;
if (status === 404 || status === 502 || status === 504) {
Message.error(response.data.message || 'Network Error');
} else if (status === 500) {
Message.error(response.statusText);
}
} else if (error.message === 'Network Error') {
Message.error(error.message);
}
return Promise.reject(false);
});
}
方法二:
// 激活码管理-导出文件
function licenseExport () {
return http({
method: 'get',
url: '/cicm/ui/v1/licenseManagement/exportModel'
})
}
licenseExport().then(res => {
// this.loadingForSearch = false
}).catch(err => {
if (err.status === 200) {
const type = 'application/gzip'
const blob = new Blob([err.data], { type: type })
const name = decodeURI(
err.headers['content-disposition'].split('=')[1])
if (window.navigator.msSaveBlob) { // 没有此判断的话,ie11下的导出没有效果
window.navigator.msSaveBlob(blob,
name)
} else {
const downloadElement = document.createElement('a')
const href = window.URL.createObjectURL(blob)
// 后台再header中传文件名
downloadElement.href = href
downloadElement.download = name
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)// 下载完成移除元素
window.URL.revokeObjectURL(href) // 释放掉blob对象
}
} else {
// this.$message.error(this.$t('ccc_pageExportErr_button'))
}
// this.loadingForSearch = false
})
更多推荐
已为社区贡献4条内容
所有评论(0)