乐趣区

关于视频:浏览器下载视频

浏览器下载视频

function _download(url, name = "file") {
    var downUrl = url;
    var fileName = name;

    var xhr = xhr = new XMLHttpRequest()
    xhr.open('GET', downUrl, true);
    xhr.responseType = 'blob';
    xhr.onload = function () {if (this.status === 200) {var type = xhr.getResponseHeader('Content-Type')
            var blob = new Blob([this.response], {type: type})
            if (typeof window.navigator.msSaveBlob !== 'undefined') {window.navigator.msSaveBlob(blob, fileName)
            } else {
                var URL = window.URL || window.webkitURL
                var objectUrl = URL.createObjectURL(blob)
                if (fileName) {
                    // 创立 a 标签用于跳转至下载链接
                    var a = document.createElement('a')
                    if (typeof a.download === 'undefined') {window.location = objectUrl} else {
                        a.href = objectUrl
                        a.download = fileName
                        document.body.appendChild(a)
                        a.click()
                        a.remove()}
                } else {window.location = objectUrl}
                URL.revokeObjectURL(objectUrl)
            }
        }
    }
    xhr.send();}
退出移动版