使用axios 发送ajax 下载文件
利用ajax下载文件。很早之前,我们下载文件,都是通过链接链接,跳转到目标页,然后下载,自从h5出来了,我们就可以用高级api 通过ajax下载了
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
link.click();
});

浙公网安备 33010602011771号