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();// 自执行点击事件

 

上述两种方式仅仅就是发送一个请求,主要依赖后端的支持;对不需要精确知道文件下载的状态,上面方式就能满足下载;

posted @ 2019-09-10 10:00  安静的女汉纸  阅读(9361)  评论(0编辑  收藏  举报