tav-ui
Version:
73 lines (68 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var base64Conver = require('./base64Conver2.js');
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 downloadByOnlineUrl(url, filename, mime, bom) {
base64Conver.urlToBase64(url).then((base64) => {
downloadByBase64(base64, filename, mime, bom);
});
}
function downloadByBase64(buf, filename, mime, bom) {
const base64Buf = base64Conver.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().includes("chrome");
const isSafari = window.navigator.userAgent.toLowerCase().includes("safari");
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.includes("?"))
url += "?download";
openWindow(url, { target });
return true;
}
exports.downloadByBase64 = downloadByBase64;
exports.downloadByData = downloadByData;
exports.downloadByOnlineUrl = downloadByOnlineUrl;
exports.downloadByUrl = downloadByUrl;
//# sourceMappingURL=download2.js.map