Flask send_from_directory 不会发送 zip 文件
Flask send_from_directory will not send zip file
我正在尝试发送存储在 static/downloads/67
中的名为 August_#1.zip
的 zip 文件。我尝试过的一切都没有奏效,没有做任何不同的事情或返回任何错误。
我已经尝试将 send_file
与所有不同类型的目录字符串一起使用,例如; static/download/67 and /Users/...
@app.route('/download')
def download():
try:
#print(directory[:-(len(name)+1)])
#print(name)
#static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
#print(static_file_dir)
#return send_from_directory(static_file_dir+"/downloads/67", "August_1.zip", as_attachment=True)
url = request.args.get('url', 0, type=str)
print(url)
d = "/Users/<...>/static/downloads/67"
return send_from_directory(d, "August_#1.zip")
except Exception as e:
#print("Error")
return str(e)
return "Nothing"
我希望在客户端网络浏览器上下载 zip 文件。
我是网络开发的新手,所以我对 Flask 不是很有信心。
谢谢。
编辑:
经过一些帮助,我发现问题出在 index.html
:
中的 button press 代码
<div class="row" style="margin-bottom: 15px;margin-right: 0;margin-left: 0;">
<div class="col-md-6" style="padding-right: 0px;padding-left: 0%;width: 100%;">
<div class="d-flex justify-content-center align-items-center" style="margin-top: 0px;width: 100%;margin-left: 0px;height: 100%;padding-top: 6px;padding-bottom: 6px;"><input class="d-flex d-xl-flex justify-content-center m-auto align-items-xl-center" type="text" style="margin: 0px;padding: 0px;margin-right: 0px;width: 90%;height: 27px;padding-left: 5px;" name="url"></div>
</div>
<div class="col-md-6" style="padding-right: 0px;padding-left: 0px;">
<div class="d-flex justify-content-center justify-content-md-start justify-content-lg-start justify-content-xl-start align-items-xl-center" style="margin-top: 0px;width: 100%;height: 100%;padding-top: 6px;padding-bottom: 6px;"><button class="btn btn-primary text-center border-dark d-flex justify-content-center" style="height: 27px;padding: 0px;background-color: rgb(255,255,255);color: rgb(14,14,14);width: 20%;min-width: 90px;" type="button", id = "download" >Download</button></div>
</div>
</div>
...
<script type=text/javascript>
$(function() {
$('#download').bind('click', function() {
$.getJSON('/download', {
url: $('input[name="url"]').val(),
});
return false;
});
});
</script>
由于前端问题,我使用JS调用函数返回文件时出错。我已经弄清楚原因了,但我设法通过在 html 中使用具有 action 属性的表单来解决问题:
<form class="row" style="margin-bottom: 15px;margin-right: 0;margin-left: 0;" action = "http://localhost:5000/download" method = "post">
我正在尝试发送存储在 static/downloads/67
中的名为 August_#1.zip
的 zip 文件。我尝试过的一切都没有奏效,没有做任何不同的事情或返回任何错误。
我已经尝试将 send_file
与所有不同类型的目录字符串一起使用,例如; static/download/67 and /Users/...
@app.route('/download')
def download():
try:
#print(directory[:-(len(name)+1)])
#print(name)
#static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'static')
#print(static_file_dir)
#return send_from_directory(static_file_dir+"/downloads/67", "August_1.zip", as_attachment=True)
url = request.args.get('url', 0, type=str)
print(url)
d = "/Users/<...>/static/downloads/67"
return send_from_directory(d, "August_#1.zip")
except Exception as e:
#print("Error")
return str(e)
return "Nothing"
我希望在客户端网络浏览器上下载 zip 文件。
我是网络开发的新手,所以我对 Flask 不是很有信心。
谢谢。
编辑:
经过一些帮助,我发现问题出在 index.html
:
<div class="row" style="margin-bottom: 15px;margin-right: 0;margin-left: 0;">
<div class="col-md-6" style="padding-right: 0px;padding-left: 0%;width: 100%;">
<div class="d-flex justify-content-center align-items-center" style="margin-top: 0px;width: 100%;margin-left: 0px;height: 100%;padding-top: 6px;padding-bottom: 6px;"><input class="d-flex d-xl-flex justify-content-center m-auto align-items-xl-center" type="text" style="margin: 0px;padding: 0px;margin-right: 0px;width: 90%;height: 27px;padding-left: 5px;" name="url"></div>
</div>
<div class="col-md-6" style="padding-right: 0px;padding-left: 0px;">
<div class="d-flex justify-content-center justify-content-md-start justify-content-lg-start justify-content-xl-start align-items-xl-center" style="margin-top: 0px;width: 100%;height: 100%;padding-top: 6px;padding-bottom: 6px;"><button class="btn btn-primary text-center border-dark d-flex justify-content-center" style="height: 27px;padding: 0px;background-color: rgb(255,255,255);color: rgb(14,14,14);width: 20%;min-width: 90px;" type="button", id = "download" >Download</button></div>
</div>
</div>
...
<script type=text/javascript>
$(function() {
$('#download').bind('click', function() {
$.getJSON('/download', {
url: $('input[name="url"]').val(),
});
return false;
});
});
</script>
由于前端问题,我使用JS调用函数返回文件时出错。我已经弄清楚原因了,但我设法通过在 html 中使用具有 action 属性的表单来解决问题:
<form class="row" style="margin-bottom: 15px;margin-right: 0;margin-left: 0;" action = "http://localhost:5000/download" method = "post">