html下载阿里云oss文件视频图片

发布时间:2024-08-04作者:小灵龙点击:155

适用性很高,html a标签,react,js调用都可以。
 
const dowloadVideo =  (url) => {  

var xhr = new XMLHttpRequest();
      xhr.open('GET', url, true);
      xhr.responseType = 'arraybuffer';    // 返回类型blob
      xhr.onload = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let blob = this.response;
            // 转换一个blob链接
            let u = window.URL.createObjectURL(new Blob([blob],{ type: 'video/mp4' }))
            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()  
}
 

标签: