标签:lin nload 模拟 name blob down let a标签 事件
function triggerclick(ele){ if(ele instanceof $){ ele = ele[0] } var clickEvent = document.createEvent("MouseEvents"); clickEvent.initEvent("click", true, true); ele.dispatchEvent(clickEvent); }
function downloadFile(url, name = "file.json") { let a = document.createElement("a") a.setAttribute("href", url) a.setAttribute("target", "_blank") a.setAttribute("download", name) let clickEvent = document.createEvent("MouseEvents"); clickEvent.initEvent("click", true, true); a.dispatchEvent(clickEvent); }
function downloadFileByJSON(json) { var blob = new Blob([JSON.stringify(json)], { type: "text/plain;charset=utf-8" }) var url = URL.createObjectURL(blob); downloadFile(url) }
function downloadFileByText(text, name) { var blob = new Blob([text], { type: "text/plain;charset=utf-8" }) var url = URL.createObjectURL(blob); downloadFile(url, name) }
标签:lin nload 模拟 name blob down let a标签 事件
原文地址:https://www.cnblogs.com/zmdblog/p/14755503.html