url链接文件转blob重命名下载

const url = "https://yunapi.aixueshi.top/api/Common/Download?encryption=login_token_3272015928c7412bb53d667bd0af8c65|20210909_6f2d67f55d2743268d62c47a1667ae9b.mp4|annex"
function xhrequest(url, callback) {
//创建XMLHttpRequest对象
var xhr = new XMLHttpRequest();
//前两个参数固定,第三个参数true时是异步,false时是同步
xhr.open("get", url, true);
//选定输出格式为blob格式
xhr.responseType = "blob";
//onload后回调this本身 onload本身不携带参数
xhr.onload = function () {
callback(this.response);
};
//关闭xhr 一定要加
xhr.send();
}
xhrequest(url, (blob) => {
console.log(blob)
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = (e) => {
const a = document.createElement('a');
a.download = 'a.mp4';
a.href = e.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
})

posted @ 2021-09-10 17:06  郭大蛋子  阅读(407)  评论(0编辑  收藏  举报