基于axios.post
的文件下载
import axios from '@/plugins/axios';
export const downloadService = {
async getExcelTemp(id: string) {
const res = await axios.post(`/app-factory/v1.0/export/formdata/template/${id}`, undefined, { responseType: 'blob' });
return res.data;
},
}
import { downloadService } from 'xxx/services';
private async downloadTemp() {
const res = await downloadService.getExcelTemp(this.processId);
const fileName = 'xxx.xlsx';
this.generateFile(res, fileName);
}
private generateFile(file: File, name?: string) {
const fileLink = document.createElement('a');
fileLink.href = window.URL.createObjectURL(file);
fileLink.download = name ?? '文件xxx.xlsx';
document.body.appendChild(fileLink);
fileLink.click();
document.body.removeChild(fileLink);
}