Skip to content Skip to sidebar Skip to footer

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:

  1. you need to write the data in utf-8
  2. you shouldn't close the GridOut instance obtained from newfile after you have written just the first chunk
  3. you should create a greenlet for each new file upload
  4. yield after writing the chunk
  5. send ack to receive next chunk also some 'id' to identify the greenlet.
  6. wake up greenlet and send the new chunk
  7. send 'end of file' once no chunks are left
  8. close the GridOut now.
  9. exit the greenlet

Post a Comment for "Streaming File Data Into Mongodb Gridfs"