【发布时间】:2014-01-31 22:52:56
【问题描述】:
期望的行为
将图像上传到 GridFS,然后在浏览器中显示(只是为了了解 GridFS 的工作原理)。
当前行为
图像上传到 GridFS 集合(我可以通过 shell 访问它),然后返回 500 错误。
错误
Error: 500 Internal Server Error
Sorry, the requested URL 'https:/mysite.com/form_action_path' caused an error:
表格
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="data" />
<input type="submit" value="submit">
</form>
瓶子
# relevant libraries
import gridfs
from bottle import response
@route('/upload', method='POST')
def do_upload():
data = request.files.data
name, ext = os.path.splitext(data.filename)
if ext not in ('.png','.jpg','.jpeg'):
return "File extension not allowed."
if data and data.file:
raw = data.file.read()
filename = data.filename
dbname = 'grid_files'
db = connection[dbname]
fs = gridfs.GridFS(db)
fs.put(raw,filename=filename)
# this is the part where I am trying to get the image back out
collection = db.fs.files
cursor = collection.find_one({"filename":"download (1).jpg"})
response.content_type = 'image/jpeg'
return cursor
return "You missed a field."
编辑:
这会在浏览器中返回一个图像:
# .... same as above
# this is the part where I am trying to get the image back out
thing = fs.get_last_version(filename=filename)
response.content_type = 'image/jpeg'
return thing
我剩下的问题是:
- 为什么初始代码不起作用?
- 如何将图片退回,以便在图片标签中使用?
- 返回图像时,返回的究竟是什么?浏览器正在解释的二进制数据?还有什么?
- 图像是否由
fs.chunks集合文档的data字段中合并的chunks组成?
【问题讨论】:
标签: mongodb python-2.7 pymongo bottle gridfs