javascript 模拟用户点击实现文件下载

2021/7/9 17:12:52

本文主要是介绍javascript 模拟用户点击实现文件下载,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

let download = (res)=>{
            // res 为blob对象
            let downloadElement = document.createElement('a');
            let href = window.URL.createObjectURL(res); //创建下载的链接
            // let type = res.type.substr(res.type.indexOf("/") + 1);
            downloadElement.href = href;
            downloadElement.download = fileName; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
 



这篇关于javascript 模拟用户点击实现文件下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程