简体   繁体   中英

send_from_directory in flask is not downloading images

I followed this tutorial and everything works except for downloading the images that were uploaded. I get 404 errors for everything I try to display in the browser. I can see the images on the hard drive just fine, but they do not download on the /uploads route. ANy ideas?

@bp.route('/uploads/<filename>')
def upload(filename):
    print('path: '+str(current_app.config['PRODUCT_UPLOAD_PATH'])+', filename: '+filename)
    return send_from_directory(current_app.config['PRODUCT_UPLOAD_PATH'], filename)

Here is the html template that loads the files. The filenames popup proeprly, but the route just shows 404 errors for all the images.

                    {% for file in files %}
                      <img src="{{ url_for('main.upload', filename=file) }}" style="width: 64px">
                    {% endfor %}

config.py to show where the directories are pointing.

    MAX_CONTENT_LENGTH = 1024 * 1024
    UPLOAD_EXTENSIONS = ['.jpg', '.png', '.gif']
    PRODUCT_UPLOAD_PATH = 'uploads/products/'

Here is the solution I found that makes this work. Thanks for the help from everyone!

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    #This string does not work
    #PRODUCT_UPLOAD_PATH = 'uploads/products/'
    #This is the proper way to configure file paths
    PRODUCT_UPLOAD_PATH = os.path.join(basedir, 'uploads', 'products')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM