视频和xml文件下载时跳转的解决办法
要注意的是文件名后面必须要加尾缀(如.mp4、.xml)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="box">点击</button>
<script>
var box=document.getElementById("box");
function downVideo (url, name){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob'; // 返回类型blob
xhr.onload = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let blob = this.response;
// 转换一个blob链接
let u = window.URL.createObjectURL(new Blob([blob]))
let a = document.createElement('a');
a.download = name;
a.href = u;
a.style.display = 'none'
document.body.appendChild(a)
a.click();
a.remove();
}
};
xhr.send()
}
box.onclick=downVideo("http://1257092661.vod2.myqcloud.com/0d1bb4cevodtransgzp1257092661/bf15d8205285890807073233140/v.f30.mp4","aaa.mp4")
</script>
</body>
</html>