Created
October 30, 2018 03:16
-
-
Save lichenbuliren/34b8a9674db235886a5ecc8667aba187 to your computer and use it in GitHub Desktop.
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
showFile(blob){ | |
// It is necessary to create a new blob object with mime-type explicitly set | |
// otherwise only Chrome works like it should | |
var newBlob = new Blob([blob], {type: "application/pdf"}) | |
// IE doesn't allow using a blob object directly as link href | |
// instead it is necessary to use msSaveOrOpenBlob | |
if (window.navigator && window.navigator.msSaveOrOpenBlob) { | |
window.navigator.msSaveOrOpenBlob(newBlob); | |
return; | |
} | |
// For other browsers: | |
// Create a link pointing to the ObjectURL containing the blob. | |
const data = window.URL.createObjectURL(newBlob); | |
var link = document.createElement('a'); | |
link.href = data; | |
link.download="file.pdf"; | |
link.click(); | |
setTimeout(function(){ | |
// For Firefox it is necessary to delay revoking the ObjectURL | |
window.URL.revokeObjectURL(data); | |
, 100} | |
} | |
fetch([url to fetch], {[options setting custom http-headers]}) | |
.then(r => r.blob()) | |
.then(showFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment