美文网首页
HTML 下载字符串为文本文件

HTML 下载字符串为文本文件

作者: ShoneSingLone | 来源:发表于2021-02-03 10:22 被阅读0次

MIME_types 媒体类型

function downloadString(text, fileType, fileName) {
  var blob = new Blob([text], { type: fileType });

  var a = document.createElement('a');
  a.download = fileName;
  a.href = URL.createObjectURL(blob);
  a.dataset.downloadurl = [fileType, a.download, a.href].join(':');
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  setTimeout(function() { URL.revokeObjectURL(a.href); }, 1500);
}

downloadString("a,b,c\n1,2,3", "text/plain", "String.txt")

相关文章

网友评论

      本文标题:HTML 下载字符串为文本文件

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