'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/'
Solution 1:[1]
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rockets4all |
