编辑:在Windows 7上使用python 2.7
我在使用mongoengine在FileField中保存数据时遇到问题。之前的文件大小&保存后差别很大。运行这个:
from mongoengine import *
class Test(Document):
File = FileField()
#connect to my mongodb
connect( 'myDB', host= '192.168.0.3' )
Test.drop_collection()
# write to mongodb GridFS, per the mongoengine documentation: https://mongoengine-odm.readthedocs.org/en/latest/guide/gridfs.html
test = Test()
test.File.put( open( r'c:\temp\owl.jpg', 'r') )
test.save()
#get the Test instance from the db, print out the number of bytes according to mongoengine
test = Test.objects().first()
print test.File.length
#just to make sure we aren't crazy, check the filesize on disk:
print os.path.getsize( r'c:\temp\owl.jpg' )
生成此输出:
864
145047
任何指针都将不胜感激!
答案 0 :(得分:1)
改变:
test.File.put( open( r'c:\temp\owl.jpg', 'r') )
为:
test.File.put( open( r'c:\temp\owl.jpg', 'rb') )
解决了这个问题。太糟糕了,操作系统之间的行为不一致。