//創建a標簽防止被瀏覽器攔截 var a = document.createElement("a"); a.setAttribute( "href", "/static/file/export/" + response.data.data ); a.setAttribute("target", "_blank"); a.click();
模擬a標簽點擊,會打開一個新的頁面一閃而過獲取文件。指向文件地址。
const filename = response.headers["content-disposition"] .split("filename=")[1] .split("; filename")[0]; const url = window.URL.createObjectURL(response.data); const link = document.createElement("a"); link.style.display = "none"; link.href = url; link.setAttribute("download", filename); document.body.appendChild(link); link.click();
獲取到文件流信息,前端處理獲取文件,不會打開新的頁面。