【问题标题】:Python Flask upload file to app folder on server (heroku)Python Flask 将文件上传到服务器上的应用程序文件夹(heroku)
【发布时间】:2017-06-23 09:53:37
【问题描述】:

我需要将图像文件上传到某个文件夹。它在 localhost 上运行良好,但如果我将应用程序推送到 heroku,它会告诉我:

IOError: [Errno 2] No such file or directory: 'static/userimg/1-bild-1.jpg'

这说明找不到文件夹?

我需要将图像文件存储几秒钟以对主题执行一些操作。之后,它们将被发送到 AWS 并从文件夹中删除。

这就是我将图像保存到文件夹中的代码:

i = 1
for key, file in request.files.iteritems():
    if file:
        filename = secure_filename(str(current_user.id) + "-bild-"+ str(i) +".jpg")
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 
        i = i + 1 

后来我从这样的文件夹中获取文件:

for item in os.listdir(os.path.join(app.config['UPLOAD_FOLDER'])):
    if item.startswith(str(current_user.id)) and item.split(".")[0].endswith("1"):
        with open(os.path.join(app.config['UPLOAD_FOLDER'], item), "rb") as thefile:
            data = base64.b64encode(thefile.read())
            upload_image_to_aws_from_image_v3('MYBUCKET', "userimg/", data, new_zimmer, "hauptbild", new_zimmer.stadt, new_zimmer.id)
        os.remove(str(thefile.name))

即app config中的上传文件夹:

UPLOAD_FOLDER = "static/userimg/"

在本地主机上一切正常。

【问题讨论】:

    标签: python heroku


    【解决方案1】:

    我不得不添加目录的绝对路径,因为服务器上的路径不同。

    您可以通过 CLI 为您的应用程序运行 heroku run bash,并使用 dir 检查目录。然后使用cd .. 返回或使用cd *directory name* 进入目录。

    我必须添加

    MYDIR = os.path.dirname(__file__)
    

    全部替换

    os.path.join(app.config['UPLOAD_FOLDER']
    

    os.path.join(MYDIR + "/" + app.config['UPLOAD_FOLDER']
    

    关于该主题的一些信息也在这里: Similar question

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 2017-02-15
    • 1970-01-01
    • 2011-10-28
    相关资源
    最近更新 更多