双鱼座的天蝎

导航

mp4文件下载,而不是在线播放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
     <button onclick="GetFile()">下载</button>
     <script>
         //会先下载流,完成后才弹出选择目录,所以最好加上进度条
         function GetFile(){
             axios({
                 url: 'http://www.xx.com/xxx.mp4',
                 method: 'get',
                 responseType: 'blob',
                 onDownloadProgress (progress){
                    //这里是下载的进度
                    console.log(Math.round(progress.loaded / progress.total * 100) + '%');
                 },
             }).then(res=>{
                 let blobUrl = window.URL.createObjectURL(res.data);
                 let link = document.createElement('a');
                 document.body.appendChild(link);
                 link.href = blobUrl;
                 link.download = '下载文件名.mp4';
                 link.click();
                 window.URL.revokeObjectURL(blobUrl);
            })
         }
     </script>
</body>
</html>

 

posted on 2023-01-13 18:00  双鱼座的天蝎  阅读(78)  评论(0编辑  收藏  举报