- async downloadFile (row) {
- const res = axios({
- url: '/downloadFileUrl',
- method: 'post',
- responseType: 'blob',
- headers: {
- 'Content-Type': 'application/json;charset=UTF-8',
- // 'Accept': 'application/octet-stream',
- },
- data: ids
- })
- .then(res => {
- if (!res.data) {
- return
- }
- // 设置类型,与后端对齐。此处请求headers中已设定类型,如果与使用一致可以不用设置类型
- //const blob = new Blob([res.data],{type: 'application/force-download'});
- const blob = res.data;
- const downloadUrl = URL.createObjectURL(blob);
- const filename = decodeURL(res.headers['content-disposition'].split("filename="))[1];
- const link = document.createElement('a');
- link.style.display = 'none';
- link.href = downloadUrl;
- link.setAttribute('download', filename);
- document.body.appendChild(link);
- link.click();
- // 释放URL对象所占资源
- URL.revokeObjectURL(downloadUrl);
- // 销毁节点
- document.body.removeChild(link);
- })
- .catch(res => {})
- }
大致文件下载如上。在使用下载时,根据需要下载文件的类型修改 type 的值进行下载即可。