我正在创建一个简单的web应用程序,您可以在其中使用瓶框架上传文章以及图像。在上传页面的HTML代码:图片上传失败在瓶框架中使用html
<html>
<body>
<form action="/created" method="POST" encrypt="multipart/form-data">
Title: <input type="text" name="title"><br>
Body: <textarea rows = 10 name="body"></textarea><br>
Choose image: <input type="file" name="image" ><br>
<input type="submit" >
</form>
</body>
</html>
我想存储在MongoDB中数据库的文章,所以,我希望将图像存储在GridFS的。对于相同的瓶子框架代码是:
@post("/created")
def created():
connect = pymongo.MongoClient(server_address)
db = connect.practiceB3
articles = db.articles
title = request.forms.get('title')
body = request.forms.get('body')
fs = gridfs.GridFS(db)
image = request.files.get('image')
img_content = image.file.read()
img_name = image.filename
document = { "title":title,"body":body}
articles.insert(document)
fs.put(img_content,filename = img_name)
return "Article successfully stored"
但是当我运行这段代码,我得到以下错误的图像部分:
Error: 500 Internal Server Error
Sorry, the requested URL 'http://localhost:8080/created.html' caused an error:
Internal Server Error
Exception:
AttributeError("'NoneType' object has no attribute 'file'",)
Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 862, in _handle
return route.call(**args)
File "/usr/local/lib/python2.7/dist-packages/bottle.py", line 1732, in wrapper
rv = callback(*a, **ka)
File "blogger.py", line 70, in created
img_content = image.file.read()
AttributeError: 'NoneType' object has no attribute 'file'
我已经在另一台机器上运行完全相同的代码它的工作完美。但在我的笔记本电脑上却失败了先谢谢你。
有趣的是,你的代码中有'img_content = image.file.read()',你的错误是指责'img_content = image.files.read()'...注意' s'? – Cyrbil
@Cyrbil谢谢你注意到这一点,我不小心上了床当我正在做一个实验将'文件'更改为'文件'以防万一以后出错时,我收到了错误信息。 – Pranjal