浏览器下载视频

浏览器下载视频

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();
}

你可能感兴趣的:(视频下载)