简体   繁体   English

烧瓶中的 send_from_directory 未下载图像

[英]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.我尝试在浏览器中显示的所有内容都出现 404 错误。 I can see the images on the hard drive just fine, but they do not download on the /uploads route.我可以很好地看到硬盘驱动器上的图像,但它们不会在 /uploads 路径上下载。 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.这是加载文件的 html 模板。 The filenames popup proeprly, but the route just shows 404 errors for all the images.文件名会正确弹出,但路径只显示所有图像的 404 错误。

                    {% 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. config.py 显示目录指向的位置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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