@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
30 lines (25 loc) • 895 B
JavaScript
/*!
* @techmely/utils
* Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com>
* MIT Licensed
*/
// src/downloadByData.ts
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);
tempLink.dataset.testid = "link-download-blob-file";
if (typeof tempLink.download === "undefined") {
tempLink.setAttribute("target", "_blank");
}
document.body.append(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
exports.downloadByData = downloadByData;
;