【发布时间】:2017-10-17 14:12:08
【问题描述】:
我想将任何上传的图像存储到名为“logo.png”的静态/自定义徽标文件夹中,无论其实际名称是什么。我有一个带有典型静态和模板文件夹的基本 Flask 设置。为简单起见,我在下面的代码中删除了扩展验证等内容。但是,这样做会引发 FileNotFound 错误。由于我想在各种环境中运行我的应用程序,我不想使用静态路径。我究竟做错了什么?感谢您的帮助。
latestfile = request.files['customlogo']
#This prints the file name of the uploaded file
print(latestfile.filename)
#I want to save the uploaded file as logo.png. No matter what the uploaded file name was.
latestfile.save(os.path.join('/static/customlogos', 'logo.png'))
【问题讨论】:
-
路径
/static/customlogos指的是文件系统上的一个目录。在这种情况下,这段代码尝试保存文件的路径是/static/customlogos/logo.png -
你创建了
/static/customlogos/吗?我也有同样的错误,我的问题是忘记创建文件夹 -
另外值得注意的是,如果文件实际上不是 PNG 文件,您将像这样重命名文件可能会导致一些问题。
-
上传的文件是png格式的图片。将上传的文件重命名为 logo.png 会解决问题吗?
-
我很确定
/static/customlogos文件夹不存在于您的文件系统中。尝试将其替换为/tmp并查看结果。
标签: python python-3.x flask