工作生活

在页面输出TXT文本

2019-07-03  本文已影响0人  RedLee666
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");
上一篇 下一篇

猜你喜欢

热点阅读