@bbon/filedownload
Version:
File download helper
61 lines (57 loc) • 2.37 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var DEFAULT_MIMETYPE = 'application/octet-stream';
var FileDownloadHelper = /** @class */ (function () {
function FileDownloadHelper() {
}
/**
* File download
*
* @param {DownloadOptions} options
* @memberof FileDownloadHelper
*/
FileDownloadHelper.prototype.download = function (options) {
if (typeof window === 'object') {
var data = options.data, contentType = options.contentType, filename = options.filename;
var contentTypeValue = contentType || 'application/octet-stream';
this.downloadFile({
data: data,
filename: filename,
contentType: contentTypeValue,
});
}
};
FileDownloadHelper.prototype.downloadFile = function (options) {
var data = options.data, filename = options.filename, mime = options.contentType, bom = options.bom;
var blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
var blob = new Blob(blobData, {
type: mime || DEFAULT_MIMETYPE,
});
var navigatorDelegate = window.navigator;
if (typeof navigatorDelegate.msSaveBlob !== 'undefined') {
navigatorDelegate.msSaveBlob(blob, filename);
}
else {
var blobURL_1 = window.URL.createObjectURL(blob);
var tempLink_1 = document.createElement('a');
tempLink_1.style.display = 'none';
tempLink_1.href = blobURL_1;
tempLink_1.setAttribute('download', filename);
tempLink_1.setAttribute('target', '_self'); // fix: WebKitBlobResource error 1
if (typeof tempLink_1.download === 'undefined') {
tempLink_1.setAttribute('target', '_blank');
}
document.body.appendChild(tempLink_1);
tempLink_1.click();
window.setTimeout(function () {
document.body.removeChild(tempLink_1);
window.URL.revokeObjectURL(blobURL_1);
}, 0);
}
};
return FileDownloadHelper;
}());
var FileDownload = FileDownloadHelper;
exports.DEFAULT_MIMETYPE = DEFAULT_MIMETYPE;
exports['default'] = FileDownload;
//# sourceMappingURL=index.cjs.js.map