Skip to content

Instantly share code, notes, and snippets.

@drugoi
Last active November 24, 2017 10:08
Show Gist options
  • Save drugoi/35848dae479999b385763f8d1e86eaca to your computer and use it in GitHub Desktop.
Save drugoi/35848dae479999b385763f8d1e86eaca to your computer and use it in GitHub Desktop.
const download = (fileName, mimeType, fileData) => {
if (window.navigator.msSaveOrOpenBlob === undefined) {
const e = document.createElement('a');
const href = `data:${mimeType};base64,${fileData}`;
e.setAttribute('href', href);
e.setAttribute('download', fileName);
document.body.appendChild(e);
e.click();
document.body.removeChild(e);
} else {
const charCodeArr = new Array(fileData.length);
for (let i = 0; i < fileData.length; ++i) {
const charCode = fileData.charCodeAt(i);
charCodeArr[i] = charCode;
}
const blob = new Blob([new Uint8Array(charCodeArr)], {
type: mimeType
});
window.navigator.msSaveOrOpenBlob(blob, fileName);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment