Sai*_*imu 39 python download flask flask-sqlalchemy
我正在尝试使用Flask创建一个Web应用程序,让用户上传文件并将其提供给其他用户.现在,我可以正确地将文件上传到upload_folder.但我似乎找不到让用户下载它的方法.
我将文件名的名称存储到数据库中.
我有一个服务于数据库对象的视图.我也可以删除它们.
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
problemes = Probleme.query.all()
if 'user' not in session:
return redirect(url_for('login'))
if request.method == 'POST':
delete = Probleme.query.filter_by(id=request.form['del_button']).first()
db.session.delete(delete)
db.session.commit()
return redirect(url_for('dashboard'))
return render_template('dashboard.html', problemes=problemes)
Run Code Online (Sandbox Code Playgroud)
在我的HTML中我有:
<td><a href="{{ url_for('download', filename=probleme.facture) }}">Facture</a></td>
Run Code Online (Sandbox Code Playgroud)
和下载视图:
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
return send_from_directory(directory=app.config['UPLOAD_FOLDER'], filename=filename)
Run Code Online (Sandbox Code Playgroud)
但它正在回归:
未找到
在服务器上找不到请求的URL.如果您手动输入了URL,请检查拼写,然后重试.
我只想将文件名链接到对象并让用户下载它(对于同一视图中的每个对象)
Mar*_*ers 48
您需要确保传递给directory参数的值是绝对路径,并针对应用程序的当前位置进行了更正.
执行此操作的最佳方法是配置UPLOAD_FOLDER为相对路径(无前导斜杠),然后通过预先设置使其成为绝对路径current_app.root_path:
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
uploads = os.path.join(current_app.root_path, app.config['UPLOAD_FOLDER'])
return send_from_directory(directory=uploads, filename=filename)
Run Code Online (Sandbox Code Playgroud)
重要的是要重申UPLOAD_FOLDER必须是相对的才能发挥作用,例如不能以a开头/.
相对路径可以工作,但过分依赖于当前工作目录设置到Flask代码所在的位置.情况可能并非总是如此.
Gau*_*gar 10
#HTML Code
<ul>
{% for file in files %}
<li> <a href="{{ url_for('download', filename=file) }}">{{ file }}</a></li>
{% endfor %}
</ul>
#Python Code
from flask import send_from_directory
app.config['UPLOAD_FOLDER']='logs'
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
print(app.root_path)
full_path = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
print(full_path)
return send_from_directory(full_path, filename)
Run Code Online (Sandbox Code Playgroud)
在烧瓶调用中下载文件。文件名为Examples.pdf, 当我点击127.0.0.1:5000/download时,它应该会下载。
例:
from flask import Flask
from flask import send_file
app = Flask(__name__)
@app.route('/download')
def downloadFile ():
#For windows you need to use drive name [ex: F:/Example.pdf]
path = "/Examples.pdf"
return send_file(path, as_attachment=True)
if __name__ == '__main__':
app.run(port=5000,debug=True)
Run Code Online (Sandbox Code Playgroud)
我也在开发一个类似的应用程序。即使文件在那里,我也收到未找到错误。这解决了我的问题。我在“static_folder”中提到了我的下载文件夹:
app = Flask(__name__,static_folder='pdf')
Run Code Online (Sandbox Code Playgroud)
我的下载代码如下:
@app.route('/pdf/<path:filename>', methods=['GET', 'POST'])
def download(filename):
return send_from_directory(directory='pdf', filename=filename)
Run Code Online (Sandbox Code Playgroud)
这就是我从 html 调用我的文件的方式。
<a class="label label-primary" href=/pdf/{{ post.hashVal }}.pdf target="_blank" style="margin-right: 5px;">Download pdf </a>
<a class="label label-primary" href=/pdf/{{ post.hashVal }}.png target="_blank" style="margin-right: 5px;">Download png </a>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
92042 次 |
| 最近记录: |