利用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(); });