问题描述
我正在研究数据库,在其中一个阶段中,我必须为有问题的对象选择图像。当我向数据库中提交新对象时,页面如下所示: New Object Image
在底部,您可以看到我的图像:“ bliss.jpg”
在此阶段,单击“提交”按钮应该可以工作,但出现此错误:*
FileNotFoundError:[错误2]没有这样的文件或目录: 'static / images \ 837ef32c-db6d-11ea-b034-186024e27139.jpg'
考虑到我的目录中有一个static/images文件夹,我认为这很奇怪。
我真的不确定我在做什么错。这是我的Python代码
@app.route("/new_camera",methods = ["GET","POST"])
def newcamera():
connection=create_connection()
if request.method =="POST":
get = request.form
Company = get["Company"]
Model = get["Model"]
PurchaseDate = get["PurchaseDate"]
Condition = get["Condition"]
KitLens = get["KitLens"]
PurchasePrice = get["PurchasePrice"]
#photo/image uploading=
Picture = request.files["Picture"]
picName = str(uuid.uuid1()) + os.path.splitext(Picture.filename)[1]
Picture.save(os.path.join("static/images",picName))
和我的HTML code
<div class="form-group">
<input name="Picture" id="Picture" type="file" accept="image/*" capture />
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-info" />
</div>
</form>
负责提交我的表格。如果您有什么办法可以尝试修复此代码,请告诉我,谢谢。
我还应该指出,我的代码中确实包含“ enctype = multipart / form-data ”。
解决方法
这是帮助诊断相对路径错误的好方法。在.save()
之前,尝试:
import os
print( os.path.abspath(__file__) ) # path to current file
print( os.path.dirname(os.path.abspath(__file__)) ) # directory containing current file
print( os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ) # up one directory
这样,您可以尝试类似的操作:
print ( os.path.join( os.path.dirname(os.path.abspath(__file__)),os.path.join("static/images",picName)) )
确保您的目录结构符合您的期望。
,您正在运行什么操作系统?
您的HTTP服务器是什么?
是否可以看到正在创建的HTTP(S)请求?
尝试做:
Picture = request.files["Picture"]
picName = str(uuid.uuid1()) + os.path.splitext(Picture.filename)[1].replace("/",""\")
Picture.save(os.path.join("static/images",picName))
我还要指出,这取决于您的HTTP服务器的设置方式。它可能不会在当前目录中主动搜索。
所以代替: 路径/到/页面/目录/静态/图像/....
它将是: / static / images /....