I want to receive a zip file as a string from an Ajax request and then hold it in memory so it can be downloaded multiple times if necessary so that only the one request is sent.
I tried to download it with this:
zip_string = 'PK etc.'
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// Start file download.
download("zip1.zip", zip_string);
It came through as a zip but then there was obviously a problem because it wouldn't open. Can anyone see what I'm doing wrong?