UNPKG

@fmdevui/fm-dev

Version:

Page level components developed based on Element Plus.

87 lines (83 loc) 3.21 kB
'use strict'; var index = require('../base64/index.js'); function downloadByOnlineUrl(url, filename, mime, bom) { index.urlToBase64(url).then((base64) => { downloadByBase64(base64, filename, mime, bom); }); } function downloadByBase64(buf, filename, mime, bom) { const base64Buf = index.dataURLtoBlob(buf); downloadByData(base64Buf, filename, mime, bom); } function downloadByData(data, filename, mime, bom) { const blobData = typeof bom !== "undefined" ? [bom, data] : [data]; const blob = new Blob(blobData, { type: mime || "application/octet-stream" }); const blobURL = window.URL.createObjectURL(blob); const tempLink = document.createElement("a"); tempLink.style.display = "none"; tempLink.href = blobURL; tempLink.setAttribute("download", filename); if (typeof tempLink.download === "undefined") { tempLink.setAttribute("target", "_blank"); } document.body.appendChild(tempLink); tempLink.click(); document.body.removeChild(tempLink); window.URL.revokeObjectURL(blobURL); } function downloadByUrl({ url, target = "_blank", fileName }) { const isChrome = window.navigator.userAgent.toLowerCase().indexOf("chrome") > -1; const isSafari = window.navigator.userAgent.toLowerCase().indexOf("safari") > -1; if (/(iP)/g.test(window.navigator.userAgent)) { console.error("Your browser does not support download!"); return false; } if (isChrome || isSafari) { const link = document.createElement("a"); link.href = url; link.target = target; if (link.download !== void 0) { link.download = fileName || url.substring(url.lastIndexOf("/") + 1, url.length); } if (document.createEvent) { const e = document.createEvent("MouseEvents"); e.initEvent("click", true, true); link.dispatchEvent(e); return true; } } if (url.indexOf("?") === -1) { url += "?download"; } openWindow(url, { target }); return true; } function openWindow(url, opt) { const { target = "__blank", noopener = true, noreferrer = true } = opt || {}; const feature = []; noopener && feature.push("noopener=yes"); noreferrer && feature.push("noreferrer=yes"); window.open(url, target, feature.join(",")); } function getFileName(headers) { var fileName = headers["content-disposition"].split(";")[1].split("filename=")[1]; var fileNameUnicode = headers["content-disposition"].split("filename*=")[1]; if (fileName?.includes("%")) fileName = decodeURIComponent(fileName); if (fileNameUnicode) { fileName = decodeURIComponent(fileNameUnicode.split("''")[1]); } return fileName; } function downloadStreamFile(res, fileName = void 0) { const contentType = res.headers["content-type"]; fileName = fileName || getFileName(res.headers); const blob = res.data instanceof Blob ? res.data : new Blob([res.data], { type: contentType }); downloadByUrl({ url: window.URL.createObjectURL(blob), fileName }); } exports.downloadByBase64 = downloadByBase64; exports.downloadByData = downloadByData; exports.downloadByOnlineUrl = downloadByOnlineUrl; exports.downloadByUrl = downloadByUrl; exports.downloadStreamFile = downloadStreamFile; exports.getFileName = getFileName; exports.openWindow = openWindow;