我正嘗試在服務器上使用django + mongoengine將視頻文件上傳到網格中。將文件數據流式傳輸到mongodb網格中
客戶端:(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)
的另一個問題是,當我嘗試寫在下一個Ajax請求下一大塊,我得到一個錯誤:
GridFSError: This document already has a file. Either delete it or call replace to overwrite it
任何幫助表示讚賞。謝謝:)