Streaming File Data Into Mongodb Gridfs
I'm trying to upload video files to gridfs using django + mongoengine on server. Client Side: (JavaScript to read/chunk the file and send the data to the server using ajax. ) _uplo
Solution 1:
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()
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.
Solution 2:
- you need to write the data in utf-8
- you shouldn't close the GridOut instance obtained from newfile after you have written just the first chunk
- you should create a greenlet for each new file upload
- yield after writing the chunk
- send ack to receive next chunk also some 'id' to identify the greenlet.
- wake up greenlet and send the new chunk
- send 'end of file' once no chunks are left
- close the GridOut now.
- exit the greenlet
Post a Comment for "Streaming File Data Into Mongodb Gridfs"