Last active
August 22, 2018 09:31
-
-
Save aindong/6669cdf72871e947640ce2d16fb68848 to your computer and use it in GitHub Desktop.
Download blob client side
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const download = blob => { | |
if (window.navigator.msSaveOrOpenBlob) { // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx | |
window.navigator.msSaveBlob(blob, "filename.csv"); | |
} else { | |
let a = window.document.createElement("a"); | |
a.href = window.URL.createObjectURL(blob, {type: "text/plain"}); | |
a.download = "filename.csv"; | |
document.body.appendChild(a); | |
a.click(); // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access | |
document.body.removeChild(a); | |
} | |
} | |
// usage | |
const blob = new Blob([csvString]); | |
download(blob) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment