Y
Y
Yorik Callil2019-06-16 16:28:25
Python
Yorik Callil, 2019-06-16 16:28:25

How to view downloaded Flask Python files after download?

I have a code (however, the standard one for loading is simple).
It allows you to select a file, everything is fine, it says that we have selected it, but
after clicking on the Upload button, it gives out: an internal server error
The server encountered an internal error and could not fulfill the request. Either the server is overloaded or an error has occurred in the application.

from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/upload', methods = ["Get","Post"])
def upload_file():
    if request.methods == "POST":
        f=request.files['the_file']
        f.save('/var/www/uploads/uploads_file.txt')
    return render_template("index2.html") #Шаблон, форма стандартная.
if __name__ == "__main__":
    app.run()

What do I need to add so that I can also view the files that I have uploaded? how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@notb, 2019-06-17
@ierikk

from flask.pocoo.org/docs/1.0/patterns/fileuploads :

file.save(...)
# после сохранения файла сделать редирект
return redirect(url_for('uploaded_file', filename=filename))

from flask import send_from_directory

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

flask.pocoo.org/docs/1.0/api/#flask.send_from_directory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question