Python 将文件数据流式传输到mongodb gridfs
我正在尝试使用服务器上的django+mongoengine将视频文件上传到gridfs 客户端:(使用JavaScript读取/分块文件,并使用ajax将数据发送到服务器。) 服务器端:Python 将文件数据流式传输到mongodb gridfs,python,python-2.7,mongoengine,gridfs,Python,Python 2.7,Mongoengine,Gridfs,我正在尝试使用服务器上的django+mongoengine将视频文件上传到gridfs 客户端:(使用JavaScript读取/分块文件,并使用ajax将数据发送到服务器。) 服务器端: def uploadVideo(request): if request.body and request.is_ajax: data = json.loads(request.body) m = Multimedia.objects.get( id = data['id'] ) m.
def uploadVideo(request):
if request.body and request.is_ajax:
data = json.loads(request.body)
m = Multimedia.objects.get( id = data['id'] )
m.media.new_file()
m.media.write( data['chunk'] )
m.media.close()
m.save()
return HttpResponse()
错误:
ERROR:django.request:Internal Server Error: /ajax/video_upload/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/praveen/Desktop/gatherify/gatherify/../ajax/views.py", line 33, in uploadVideo
m.media.write( data['chunk'] )
File "/usr/local/lib/python2.7/dist-packages/mongoengine-0.8.7-py2.7.egg/mongoengine/fields.py", line 1172, in write
self.newfile.write(string)
File "build/bdist.linux-i686/egg/gridfs/grid_file.py", line 327, in write
"order to write %s" % (text_type.__name__,))
TypeError: must specify an encoding for file in order to write unicode
我不知道如何指定编码,官方文档对此没有任何说明。()
另一个问题是,当我尝试在下一个ajax请求上编写下一个块时,我会遇到一个错误:
GridFSError: This document already has a file. Either delete it or call replace to overwrite it
感谢您的帮助。谢谢:)在将
数据['chunk']
字符串写入文件字段之前对其进行编码
m.media.new_file()
m.media.write( data['chunk'].encode("UTF-8") )
m.media.close()
至于第二个问题,您已经在gridfs中创建了一个文件。正如错误消息所说,您必须m.media.delete()
it或m.media.replace()
it。如果要附加它,可能必须将文件内容作为字符串添加到m.media.get()
中,将新块添加到字符串中,然后创建一个新的m.media
gridfs文件。您不能直接编辑gridfs内容
m.media.new_file()
m.media.write( data['chunk'].encode("UTF-8") )
m.media.close()