js 将数据保存到本地

2020-08-28 03:26:22 浏览数 (1)

  • name 文件名
  • data 数据
  • type mime类型
代码语言:javascript复制
saveAs: function (name, data, type) {
    const element = document.createElementNS("http://www.w3.org/1999/xhtml", "a"),
        ev = document.createEvent("MouseEvents"),
        urlObject = window.URL || window.webkitURL || window,
        export_blob = new Blob([data], {type: type ? type : ''});
    element.href = urlObject.createObjectURL(export_blob);
    element.download = name;
    ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    element.dispatchEvent(ev);
    urlObject.revokeObjectURL(element.href);
    element.href = '';
}

0 人点赞