美文网首页工作生活
在页面输出TXT文本

在页面输出TXT文本

作者: RedLee666 | 来源:发表于2019-07-03 09:55 被阅读0次
function download(filename, content, contentType) {
     //检查格式,
    if (!contentType)
        contentType = 'application/octet-stream';
    //创建a标签
    var a = document.createElement('a');
    //创建Blob对象
    var blob = new Blob([content], { 'type': contentType });
    让a标签指向Blob对象
    a.href = window.URL.createObjectURL(blob);
    //设置文件名
    a.download = filename;
    //触发点击事件
    a.click();
}
//调用示例
download("data.txt", "hello world");

相关文章

网友评论

    本文标题:在页面输出TXT文本

    本文链接:https://www.haomeiwen.com/subject/yiifhctx.html