当我在 Azure 上部署 Flask 应用程序时,视图会升起
TypeError: send_from_directory() missing 1 required positional argument: 'path'
。当我在本地运行时,不会发生这种情况。
from flask import send_from_directory
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
def download(filename):
uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
return send_from_directory(directory=uploads, filename=filename)
将最后一行更改为
return send_from_directory(uploads, filename)
。
send_from_directory
的 Flask 文档。底部的变更日志显示“2.0 版本中已更改:path
替换了 filename
参数。”
如果您仍想使用命名参数,请将
filename=
更改为 path=
。 send_from_directory(directory=uploads, path=filename)
return send_from_directory(directory=uploads, filename=filename)
更改为
return send_from_directory(directory=uploads, path=filename, as_attachment=True)
就我而言,我需要证书,它保存在 static/pdf/certificates 中 文件夹
@app.route('/download/<filename>', methods = ["GET", "POST"])
def download(filename):
uploads = os.path.join(current_app.root_path, "static/pdf/folder_name")
return send_from_directory(directory=uploads,path=filename,as_attachment=True)
使用
send_file
代替 send_from_directory
from flask import send_file
@app.route("/download")
def download():
return send_file('<file_path>', as_attachment=True)
请始终记住您的文件是否位于
app.py
所在的当前目录中,然后不要给出完整路径,只需输入文件名。但是,如果您的文件位于 app.py
文件所在的文件夹中,则使用如下所示的文件夹名称来命名,
@district_admin_bp.route("/download_pc")
def download_pc():
filename = "pc.xlsx"
return send_file("static\\report_upload\\pc_master\\"+filename, as_attachment=True)