Tomás MARMAY
Tomás MARMAY

Reputation: 17

How to return image saved in database (Flask)

So I'm trying to make a upload route to save in a database.I done it, but now I want to return this image (To can see it and if I wish download) in another route and flask return me this error:"name 'send_file' is not defined". What I'm doing wrong ? Is there a better way to do it ?

@app.route('/upload', methods=['GET','POST'])
@login_required
def upload():
        
    if request.method == "POST":

        if not allowed_image_size(request.cookies.get("filesize")):
            return redirect(request.url)

        file = request.files['image']
        comentaries = request.form['comentaries']

        if not allowed_image(file.filename):
            return('No valid extencion')
            #return redirect(request.url
        else: 
            filename = secure_filename(file.filename)

        newFile = FileContent(client_name=current_user.username, comments=comentaries, data=file.read())
        db.session.add(newFile)
        db.session.commit() 
        return 'Saved ' + file.filename + ' to the database'    
    return render_template('upload.html')

@app.route('/download')
def download():
    file_data = FileContent.query.all()
    return send_file(BytesIO(file_data.data), attachment_filname='egreso.jpg', as_attachment=True)

Upvotes: 1

Views: 530

Answers (1)

Nour-Allah Hussein
Nour-Allah Hussein

Reputation: 1449

The error you face says "name 'send_file' is not defined".

This mentions that you should import the send_file function from flask like:

from flask import send_file

Upvotes: 1

Related Questions