javascript - 如何使用烧瓶和 ajax 将磁盘上的 zip 文件发送到客户端?
问题描述
我看到了这个问题,但我不能使用那个答案,因为 ZipFile 是在另一个脚本中生成的。在烧瓶应用程序中,我访问保存在磁盘上的那个 zip 文件。
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
然后在客户端:
$.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"]);
}
});
当我尝试打开 zip 文件时出现此错误(说明它不能作为 zip 文件打开):
如何将保存在磁盘上的 zip 文件发送到客户端?如何在烧瓶上设置并在客户端生成 blob?
解决方案
推荐阅读
- sql-server - 如何将集合从 TFS 2017 迁移到 Azure DevOps Server 2019(不同的数据库服务器)
- c# - AdaptiveCard Bot Framework V4 中的日文字符
- visual-studio-code - 使用 Visual Studio Code 的 REST 客户端扩展的 POST 请求
- javascript - 多次显示警报
- javascript - 如何解决这个 CSS 不是函数错误
- php - 向除发件人以外的所有用户发布消息
- r - 直接从 VisEvents 调用 R 函数
- javascript - 在浏览器中安装 AdBlock 扩展时,如何使用 Blob 在新页面中显示 pdf\jpg 文件
- c# - 如何通过列表
从 C# 到 C++ CLi? - c - 了解如何使用 C 中的位运算符计算数字的尾随零