Skip to content

Instantly share code, notes, and snippets.

@mitrofun
Forked from raymondpittman/download_zip.js
Created September 14, 2020 14:01
Show Gist options
  • Select an option

  • Save mitrofun/758d2d7be0bc1a800891b4f903f18a2a to your computer and use it in GitHub Desktop.

Select an option

Save mitrofun/758d2d7be0bc1a800891b4f903f18a2a to your computer and use it in GitHub Desktop.
Download a .zip file from a blob URL using Javascript with Fetch.
/*
* @Author Raymond Pittman
* @Github: https://github.com/raymondpittman
*/
function download(url, filename) {
fetch(url, {
mode: 'no-cors' /*{mode:'cors'}*/
}).then((transfer) => {
return transfer.blob();
}).then((bytes) => {
let elm = document.createElement('a');
elm.href = URL.createObjectURL(bytes);
elm.setAttribute('download', filename);
elm.click()
}).catch((error) => {
console.log(error);
})
}
download('http://.zip', 'test.zip');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment