Last active
July 13, 2022 06:37
-
-
Save DearVikki/12b0271fae4faad9ad377b9f49f781ec to your computer and use it in GitHub Desktop.
Files
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
// this is another one |
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
var request = new XMLHttpRequest(); | |
request.open('POST', $someUrl, true); | |
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); | |
request.responseType = 'blob'; | |
request.onload = function() { | |
// Only handle status code 200 | |
if(request.status === 200) { | |
// Try to find out the filename from the content disposition `filename` value | |
var disposition = request.getResponseHeader('content-disposition'); | |
var matches = /"([^"]*)"/.exec(disposition); | |
var filename = (matches != null && matches[1] ? matches[1] : 'file.pdf'); | |
// The actual download | |
var blob = new Blob([request.response], { type: 'application/pdf' }); | |
var link = document.createElement('a'); | |
link.href = window.URL.createObjectURL(blob); | |
link.download = filename; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
// some error handling should be done here... | |
}; | |
request.send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment