js中保存.txt文件下载

function download(filename, text) {
var element = document.createElement(‘a’);
element.setAttribute(‘href’, ‘data:text/plain;charset=utf-8,’ + encodeURIComponent(text));
element.setAttribute(‘download’, filename);

element.style.display = ‘none’;
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
}

download(“hello.txt”,“This is the content of my file ?”);

发布了55 篇原创文章 · 获赞 2 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/wqs15192095633/article/details/93855668
今日推荐