瀏覽器會默認打開圖片/pdf/txt文件,可通過以下方法將url轉(zhuǎn)為文件流再保存本地
fetch(url, {
responseType: 'blob'
}).then(res => {
const url = window.URL.createObjectURL(new Blob([res.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', name);
link.click();
});