首页 资源列表 文章列表

js实现视频是下载而不是打开

function download(url) {
        var timestamp = new Date().getTime();
        let filename = timestamp + ".mp4"
        fetch(url)
            .then(response => response.blob())
            .then(blob => {
                const blobUrl = window.URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = blobUrl;
                a.download = filename;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                window.URL.revokeObjectURL(blobUrl);
            })
            .catch(error => console.error('下载失败:', error));
    }