将文件数据流式传输到mongodb gridfs [英] Streaming file data into mongodb gridfs

查看:603
本文介绍了将文件数据流式传输到mongodb gridfs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用服务器上的django + mongoengine将视频文件上传到gridfs.

I'm trying to upload video files to gridfs using django + mongoengine on server.

客户端:( JavaScript读取/打包文件,然后使用ajax将数据发送到服务器.)

_upload : function() {
    chunk = self.file.slice( self.start, self.end );
    reader = new FileReader();
    reader.readAsDataURL( chunk );
    reader.onload = function(e) {
        this.request = new XMLHttpRequest();
        this.request.open( 'POST', '/ajax/video_upload/' );
        this.request.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        this.request.overrideMimeType('application/octet-stream');
        this.request.send( JSON.stringify({ 'chunk': e.target.result, 'id' : self.file_id }) );
        this.request.onload = function() {

        if( self.start >= self.file_size && self.preventedOverflow ) {
        return;
        }

        self.start = self.end;
        self.end = self.end + self.chunkSize;

       self._upload();
    };
}

服务器端:

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

我不知道如何指定编码,官方文档对此没有说明. ( http://mongoengine-odm.readthedocs.org/guide/gridfs.html)

I have no idea how to specify the encoding the official documentation doesn't say anything about it. (http://mongoengine-odm.readthedocs.org/guide/gridfs.html)

另一个问题是,当我尝试在下一个ajax请求上写入下一个块时,出现错误:

Another problem is that when I try to write the next chunks on the next ajax request I get an error:

GridFSError: This document already has a file. Either delete it or call replace to overwrite it

感谢您的帮助.谢谢:)

Any help is appreciated. Thanks :)

推荐答案

data['chunk']字符串进行编码,然后再将其写入FileField.

Encode the data['chunk'] string before writing it to the FileField.

m.media.new_file()
m.media.write( data['chunk'].encode("UTF-8") )
m.media.close()

关于第二个问题,您已经在gridfs中创建了一个文件.就像错误消息指出您必须m.media.delete()m.media.replace(<a new gridfs entry>)一样.如果要附加它,则可能必须将文件内容作为字符串m.media.get()附加到字符串中,然后创建一个新的m.media gridfs文件.您不能直接编辑gridfs内容.

As for your second question you've already created a file in gridfs. Like the error message says you've got to m.media.delete() it or m.media.replace(<a new gridfs entry>) it. If you're looking to append it you're probably going to have to m.media.get() the file contents as a string, append the new chunk to the string, then create a new m.media gridfs file. You can't edit gridfs content directly.

这篇关于将文件数据流式传输到mongodb gridfs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆