// Save and Open blob file with provided blob and filename export const downloadBlob = (blob, fileName = null, newWindow = false) => { if (window.navigator && window.navigator.msSaveOrOpenBlob) { if (fileName) { window.navigator.msSaveBlob(blob, fileName); } else { window.navigator.msSaveBlob(blob); } } else { const dlink = document.createElement('a'); dlink.rel = 'noopener'; dlink.href = window.URL.createObjectURL(blob); if (fileName) { dlink.download = fileName; } if (newWindow) { const href = dlink.href; window.open(href, fileName); } else { document.body.appendChild(dlink); dlink.click(); // Revoking is tricky: // https://code.google.com/p/chromium/issues/detail?id=375297#c7 window.setTimeout(() => { const href = dlink.href; URL.revokeObjectURL(href); document.body.removeChild(dlink); }, 1000); } } }; // Dynamically create a blob to open and save, given file name, and file contents // Note: You can supply a mime type, otherwise, default will be text/plain export const downloadFile = (fileName, contents, mimeType) => { mimeType = mimeType || 'text/plain'; const blob = new Blob([contents], { type: mimeType }); downloadBlob(blob, fileName); };