UNPKG

vanillajs-browser-helpers

Version:

Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS in the browser

32 lines (31 loc) 974 B
import isBlob from './isBlob'; const MSSave = 'msSaveBlob' in navigator; export function fileSavingSupported() { return 'Blob' in window && (MSSave || 'download' in HTMLAnchorElement.prototype); } /** * Creates a file with given content and triggers download. * (if the browser doesn't support file downloads, this method does nothing) * * @param content - The content of the file * @param name - The name to give the downloaded file * @param type - The content type of the file */ export default function saveFile(content, name, type = 'text/plain; charset=utf-8') { if (!fileSavingSupported()) { return; } if (!isBlob(content)) { content = new Blob([`${content}`], { type }); } if (MSSave) { navigator.msSaveBlob(content); } else { const a = document.createElement('a'); a.download = name; a.target = '_blank'; a.href = URL.createObjectURL(content); a.click(); } }