烧瓶下载文件

我正在尝试用 Flask 创建一个网络应用程序,让用户上传一个文件并将它们提供给另一个用户。现在,我可以正确地把文件上传到 上传 _ 文件夹。但我似乎找不到让用户下载回来的方法。

我正在将文件名存储到数据库中。

我有一个服务数据库对象的视图。我也可以删除它们。

@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)

在我的 HTML 中,我有:

<td><a href="{{ url_for('download', filename=probleme.facture) }}">Facture</a></td>

以及下载视图:

@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
return send_from_directory(directory=app.config['UPLOAD_FOLDER'], filename=filename)

但它正在回归:

没找到

在服务器上找不到请求的 URL。如果您手动输入网址,请检查您的拼写并再试一次。

我只想将文件名链接到对象,并让用户下载它(对于同一视图中的每个对象)

238084 次浏览

You need to make sure that the value you pass to the directory argument is an absolute path, corrected for the current location of your application.

The best way to do this is to configure UPLOAD_FOLDER as a relative path (no leading slash), then make it absolute by prepending 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)

It is important to reiterate that UPLOAD_FOLDER must be relative for this to work, e.g. not start with a /.

A relative path could work but relies too much on the current working directory being set to the place where your Flask code lives. This may not always be the case.

I was also developing a similar application. I was also getting not found error even though the file was there. This solve my problem. I mention my download folder in 'static_folder':

app = Flask(__name__,static_folder='pdf')

My code for the download is as follows:

@app.route('/pdf/<path:filename>', methods=['GET', 'POST'])
def download(filename):
return send_from_directory(directory='pdf', filename=filename)

This is how I am calling my file from 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>

To download file on flask call. File name is Examples.pdf When I am hitting 127.0.0.1:5000/download it should get download.

Example:

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)
#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)