js利用 iframe 或 a 標簽連接下載文件


iframe標簽方法下載:

var downloadFileUrl = "http://localhost:3000" var elemIF = document.createElement("iframe"); elemIF.src = downloadFileUrl; elemIF.style.display = "none"; document.body.appendChild(elemIF);

a標簽方法下載:

const a = document.createElement('a'); // 創建a標簽
a.setAttribute('download', '');// download屬性
a.setAttribute('href', downloadFileUrl);// href鏈接 
a.click();// 自執行點擊事件

 

上述兩種方式僅僅就是發送一個請求,主要依賴后端的支持;對不需要精確知道文件下載的狀態,上面方式就能滿足下載;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM