本文主要是介绍vue项目从后端下载文件显示进度条或者loading,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//API接口
export const exportDownload = (params?: Object, peCallback?: Function) => {return new Promise((resolve, reject) => {axios({method: 'get',url: '',headers: {'access_token': `${getToken()}`,},responseType: 'blob',params,onDownloadProgress: (pe) => {peCallback ? peCallback(pe) : '';},}).then((res) => {resolve(res);}).catch((err) => {console.warn(err);});});
};
function peCallback(pe) {// 下载回调
//百分比计算defaultPercent.value = Math.round((pe.event.loaded / pe.event.total) * 100);if (pe.event.loaded === pe.event.total) {showProgress.value = false;message.success({ content: '下载完成', key, duration: 3 });}}try {exportDownload(params peCallback).then((res) => {// 接口3const fileName = 'xxx.xlsx';let bold = new Blob([res.data]);const newUrl = window.URL.createObjectURL(bold);const link = document.createElement('a');link.href = newUrl;link.setAttribute('download', fileName);document.body.appendChild(link);link.click();link.parentNode?.removeChild(link);});} catch (error) {message.warn('下载失败,请重试');}
这篇关于vue项目从后端下载文件显示进度条或者loading的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!