+
95
-

回答

三种方式:

1.直接跳转:

window.location.href=url;

或者这样

//路径
var url = ""
// 会打开一个空白页下载,然后空白页消失,用户体验不好
function download1() {
window.open(url);
}

2.通过构造a标签

创建a下载标签模拟点击下载
// 直接下载,用户体验好
function download2() {
const link = document.createElement('a');
link.setAttribute('href', url); //设置下载文件的url地址
//link.setAttribute('download', 'download.mp4'); //用于设置下载文件的文件名
link.click();
}
download2()
3、js创建iframe实现多文件下载代码:
//数组路径
let downloadUrls = [];
function loadFilesWin(urlsArray) {
for (let i = 0; i < urlsArray.length; i++) {
const iframe = document.createElement("iframe");
iframe.style.display = "none"; // 防止影响页面
iframe.style.height = 0; // 防止影响页面
iframe.src = urlsArray[i];
document.body.appendChild(iframe); // 这一行必须,iframe挂在到dom树上才会发请求
}
}
loadFilesWin(downloadUrls)



网友回复

我知道答案,我要回答