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