I saw this question, but I can't use that answer since the ZipFile is generated in another script. In the flask app, I access that zip file saved on the disk.
if download_local:
file_name = "adyenfiles.zip"
file_path = os.path.join(app.config["tmp_path"], file_name).replace('\\', '/')
logging.info(f"Enviando arquivo: {file_path}")
logging.info(f"FILE_PATH: {file_path}")
response = make_response(send_file(file_path, as_attachment=True, mimetype="application/zip"))
response.headers["Content-Disposition"] = f"attachment;filename={file_name}"
return response
Then on the client:
$.ajax({
type: "POST",
url: url,
data: $("#manual-download-form").serialize(),
beforeSend: function () {
$("#request-download-progress").show();
displayMessage("Processo de requisição de download iniciado, por favor aguarde.");
},
success: function (response, status, xhr) {
$("#manual-download-form")[0].reset();
$("input[name='date-group']:checked").val("specific");
$("#date-input-area").html(
`
<div class="input-field col s12">
<input id="manual-download-datepicker" name="specific-date" type="text" class="datepicker">
<label for="specific-date">Data*</label>
</div>
`
);
resetDatepicker();
var filename = "";
var disposition = xhr.getResponseHeader('Content-Disposition');
if (disposition && disposition.indexOf('attachment') !== -1) {
var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(disposition);
if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
}
var type = xhr.getResponseHeader('Content-Type');
var blob = new Blob([response], { type: "application/zip" });
if (typeof window.navigator.msSaveBlob !== 'undefined') {
// IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
window.navigator.msSaveBlob(blob, filename);
} else {
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);
if (filename) {
// use HTML5 a[download] attribute to specify filename
var a = document.createElement("a");
// safari doesn't support this yet
if (typeof a.download === 'undefined') {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
}
} else {
window.location = downloadUrl;
}
setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
}
},
complete: function (response, status, xhr) {
$("#request-download-progress").hide();
displayMessage("Se necessário, verifique o diretório para certificar-se de que os arquivos foram baixados.");
},
error: function (response) {
displayMessage(response["responseText"]);
}
});
I get this error when I try to open the zip file (Stating that it can't be open as a zip file):
How can I send a zip file that is saved on the disk to the client?? How do I set it up on flask and generate the blob on the client?