纯JS生成并下载各种文本文件或图片

本文转载自https://juejin.im/post/5bd1b0aa6fb9a05d2c43f004

var text=“想要下载的文本,字符串等资源”;
var funDownload=function(content,filename){
    var _a=document.createElement('a');
    _a.download=filename;
    _a.style.display=none;
    var blob=new Blob([content]);
    _a.href=URL.createObjectURL(blob);
    document.body.appendChild(_a);
    _a.click();
    document.body.removeChild(_a);
}
if('download' in document.createElement('a')){
    funDownload(text,'text.txt');       //text.txt是你将要下载的资源的名称
}else{
    alert('浏览器不支持');
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容