Skip to content

Instantly share code, notes, and snippets.

@urbontaitis
Created January 21, 2018 16:23
Show Gist options
  • Save urbontaitis/427cf6445016f728871d0595f2e96a2e to your computer and use it in GitHub Desktop.
Save urbontaitis/427cf6445016f728871d0595f2e96a2e to your computer and use it in GitHub Desktop.
handleDownload = (e) => {
e.preventDefault()
axios({
method: 'get',
url: 'http://test.net/backend/api/orders/20170601_53/items/51/attachments/26',
responseType: 'arraybuffer'
}).then( (response) => {
var blob = new Blob([response.data], {type: response.headers['content-type']});
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(blob, 'filename.png');
} else {
var blobURL = window.URL.createObjectURL(blob);
var tempLink = document.createElement('a');
tempLink.style.display = 'none';
tempLink.href = blobURL;
tempLink.setAttribute('download', 'filename.png');
tempLink.setAttribute('target', '_blank');
document.body.appendChild(tempLink);
tempLink.click();
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment