JS下载txt/json等并放入内容

封装

  • 代码
function fakeClick(obj) {
  var ev = document.createEvent("MouseEvents");
  ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  obj.dispatchEvent(ev);
}

function exportRaw(name, data) {
  var urlObject = window.URL || window.webkitURL || window;
  var export_blob = new Blob([data]);
  var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
  save_link.href = urlObject.createObjectURL(export_blob);
  save_link.download = name;
  fakeClick(save_link);
}

  • 用法
exportRaw(string,string);
exportRaw('address-element.json',JSON.stringify(data))
上一篇:【报告分享】2021高端纯电动车洞察报告—高端EV就像一朵带刺的玫瑰-易车(附下载)


下一篇:vue 自定义video 的进度条,可快进,可拖动