【问题标题】:How to retrieve file's with grid fs如何使用 gridfs 检索文件
【发布时间】:2014-03-29 14:24:33
【问题描述】:

我已经用 python 在 mongodb 中插入了带有 gridfs 的图像文件,我想用另一个函数检索该文件。我怎样才能检索文件。我正在使用 djanog 和 python(2.7)。提前致谢!

def file_grid(request):
   datafile = open('jobs.jpg',"r");
   thedata = datafile.read()
   fs = gridfs.GridFS(db)
   stored = fs.put(thedata, filename="testimage")
   return HttpResponse("inserted")

【问题讨论】:

    标签: python django image mongodb gridfs


    【解决方案1】:
    fs = gridfs.GridFS(db)
    gridout = fs.get_last_version("testimage")
    

    gridout 对象是GridOut 的实例,用于读取文件。您可以使用gridout.read() 一次获取所有字节,或者遍历字节块,例如:

    for chunk in gridout:
        do_something_with(chunk)
    

    GridFS 块默认约为 256k。

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2014-11-08
      • 2013-01-02
      • 2016-02-14
      • 1970-01-01
      • 2017-07-04
      • 2013-11-30
      • 2013-12-18
      • 2020-11-27
      相关资源
      最近更新 更多