js實現瀏覽器下載視頻


第一種,這種比較節能,如果視頻比較大。不會造成卡頓

fetch('你的視頻地址.mp4').then(res => res.blob()).then(blob => {
 const a = document.createElement('a');
  document.body.appendChild(a)
  a.style.display = 'none'
  const url = window.URL.createObjectURL(blob);
  a.href = url;
  a.download = '視頻.mp4';
  a.click();
  document.body.removeChild(a)
  window.URL.revokeObjectURL(url);
})

第二種,此方法適合url地址中不帶參數的地址,和text,wprd,pdf,等文件

demo() {
            var url = 'http://1305935074.vod2.myqcloud.com/19b93487vodcq1305935074/393c276c3701925920977239345/f0.mp4'
            var xhr = new XMLHttpRequest()
            xhr.open('get', url, true) // 也可以使用POST方式,根據接口
            xhr.responseType = 'blob' // 返回類型blob

            xhr.onload = function () {
                if (this.status === 200) {
                    var blob = this.response
                    var reader = new FileReader()

                    reader.readAsDataURL(blob) // 轉換為base64,可以直接放入a表情href
                    reader.onload = function (e) {
                        var a = document.createElement('a')
                        a.download = '下載文件名' //下載文件名
                        a.href = e.target.result
                        a.click()
                        window.URL.revokeObjectURL(e.target.result)
                    }
                }
            }
            xhr.send()
        },

 

 

參考文章:https://blog.csdn.net/weixin_42981560/article/details/115507234


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM