不打开新窗口进行下载
function download(res) {
var elemIF = document.createElement("iframe");
elemIF.src = res;
elemIF.style.display = "none";
document.body.appendChild(elemIF);
}
window.open(url, '_blank');
a标签
const ele = document.createElement('a'); //新建一个a标签
ele.setAttribute('href', url);
ele.click();
blob
fetch(url).then((res) =>
res.blob().then((blob) => {
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name; // 下载名称
a.click();
window.URL.revokeObjectURL(url);
})
);
- <!DOCTYPE html>
- <html>
- <head>
- <title>视频下载</title>
- <script src="https:0.min.js"></script>
- </head>
- <body>
- <h1>视频下载</h1>
- <button onclick="downloadVideo()">下载视频</button>
-
- <script>
- function downloadVideo() {
- var videoUrl = "https://xxx.com//out/aaa.mp4"; // 替换为你要下载的视频的URL
- var fileName = "video.mp4"; // 下载的文件名
-
- // 创建一个隐藏的a标签
- var a = document.createElement('a');
- a.style.display = 'none';
- document.body.appendChild(a);
-
- // 使用XMLHttpRequest下载视频
- var xhr = new XMLHttpRequest();
- xhr.open('GET', videoUrl, true);
- xhr.responseType = 'blob';
-
- xhr.onload = function() {
- if (xhr.status === 200) {
- // 将视频Blob对象创建一个临时URL
- var videoBlob = xhr.response;
- var url = window.URL.createObjectURL(videoBlob);
-
- // 设置a标签的属性,并触发点击事件进行下载
- a.href = url;
- a.download = fileName;
- a.click();
-
- // 释放URL对象
- window.URL.revokeObjectURL(url);
- }
- };
-
- xhr.send();
- }
- </script>
- </body>
- </html>