@zodash/save-as
Version:
Save Blob or Text as File
34 lines (33 loc) • 1.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveAs = exports.isBlob = void 0;
function isBlob(data) {
return Object.prototype.toString.call(data) === '[object Blob]';
}
exports.isBlob = isBlob;
async function saveAs(blobOrText, filename = `${Date.now()}`) {
const blob = isBlob(blobOrText)
? blobOrText
: new Blob([blobOrText], { type: 'text/html' });
const dataUrl = window.URL.createObjectURL(blob);
const $link = document.createElement('a');
$link.style.display = 'none';
$link.href = dataUrl;
$link.download = filename;
document.body.appendChild($link);
if ($link) {
$link.click();
}
else {
$link.target = '_blank';
$link.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: true,
cancelable: true,
}));
}
window.URL.revokeObjectURL(dataUrl);
document.body.removeChild($link);
}
exports.saveAs = saveAs;
exports.default = saveAs;