Skip to content

Instantly share code, notes, and snippets.

@mystikraz
Created March 13, 2022 10:49
Show Gist options
  • Select an option

  • Save mystikraz/6317b6d3c23f6986bcf233fd500ef05b to your computer and use it in GitHub Desktop.

Select an option

Save mystikraz/6317b6d3c23f6986bcf233fd500ef05b to your computer and use it in GitHub Desktop.
download files, csv, pdf, excel, zip files using ajax javascript, jquery
async function getZipAsync(form) {
let response = await fetch("/BuyerReport/NewCharges" + '?' + (new URLSearchParams($(form).serialize())).toString());
let res = await response.blob();
if (res.type === 'application/zip') {
const downloadUrl = window.URL.createObjectURL(res);
const link = document.createElement('a');
link.setAttribute('href', downloadUrl);
link.setAttribute('download', 'new charges report');
link.style.display = 'none';
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(link.href);
document.body.removeChild(link);
} else {
alert("Your bulk export is larger than 50 MB. It will be sent as a link to your email address.");
}
showHideSpinner(false);
}
function download(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment