mongodb - Mongodb GridFS读取返回空内容
问题描述
当我使用 mongodb 的 python api mongoengine 从 GridFS 读取文件时,第一次读取后我得到了空内容。这是我尝试过的:
class Test(Document):
file=FileField()
a = Test(id=id)
print a.file.read() # has expected output
print a.file.read() # empty
print a.file.read() # empty`
所以我很好奇 GridFS 读取是如何工作的,提前感谢任何提示:)
解决方案
我们将不得不改进相应的文档,因为它是一个常见的混淆来源,但是一旦你阅读了它,你需要“倒带”它。
IE:
class Test(Document):
file = FileField()
a = Test(id=id)
print a.file.read() # has expected output
a.file.seek(0)
print a.file.read()
请注意,这并非特定于 MongoEngine,而是底层 GridFS/GridOut 类文件对象的行为方式
推荐阅读
- javascript - express/node 中带下划线的命令
- javascript - NodeJS:承诺没有回应
- c# - ViewDataDictionary 模型类型异常
- windows - 如何检查WMIC是否输出空字符串或请求的Windows原始产品密钥?
- sage - 用矩阵组合集合:sagemath
- spring-webflux - 使用spring响应式webClient面临问题“WebClientRequestException:挂起的获取队列已达到其最大大小1000”
- php - 重定向和 reauth=1 的奇怪 wp-admin 登录问题
- flutter - 使用 Flutter 从一个未来获取价值并在另一个未来使用它
- javascript - 当 JSON.stringify 显示空对象但实际上它不是空的时如何获取实际内容
- javascript - 鼠标悬停时的 Blazor 运动?