download a file from mongodb database

4,433 views
Skip to first unread message

Bralima Branding

unread,
May 12, 2012, 5:33:56 PM5/12/12
to mongodb-user
Hey,
can someone help me. I am using a mongodb database and gridfs. I can
upload files to my database, but I don't know how to download those
files.

I'm just starting with mongodb, and I am using coffeescript.

Thanks for your help

Eliot Horowitz

unread,
May 12, 2012, 11:07:49 PM5/12/12
to mongod...@googlegroups.com
This really depends on your setup.
When you say coffeescript, do you mean server side or client side?
If server side, what server?
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Saar Korren

unread,
May 13, 2012, 10:09:23 AM5/13/12
to mongodb-user
You need some script which can serve static files from the database.
The simplest set-up is to have a script which takes a filename as
input, and retrieves the file's contents, flushing those to the
output. It is recommended to use a driver which allows reading the
file as a stream to save on memory. For instance, the Java driver can
return the file's content as an InputStream, while the PHP driver can
only return the entire file as a single memory chunk.

The issue with the above method is that it skips important static file
specification details used for caching, such as ETag and If-Modified
headers. These are not strictly required for the site to work, but it
would cause a lot of load to skip those, as it would cause the browser
not to cache any of the served files.

Unfortunately, most server frameworks do not have a ready-made static
file service which merely requires plugging the file data in. Chances
are you will have to follow the specifications and write a lot of the
related code yourself. There is a very good static files servlet
example for Java here:
http://balusc.blogspot.com/2009/02/fileservlet-supporting-resume-and.html

You may be able to adapt that code to work with your own language and
server. Provided you can read Java, at least.

Bralima Branding

unread,
May 13, 2012, 12:09:44 PM5/13/12
to mongod...@googlegroups.com
Hi,

Thanks for your contributions.
Eliot, I am using coffescript in the server with nodejs in a linux server.I have a mongodb Database, I ear about one tool called  nginx-gridfs, but I still can not find tutorials on how to use it.

Saar, I checked the link that you provide, but I didn't get this java code...

Eliot Horowitz

unread,
May 14, 2012, 12:41:45 AM5/14/12
to mongod...@googlegroups.com
There are a number of options for serving files from node.js.

http://mongodb.github.com/node-mongodb-native/markdown-docs/gridfs.html

https://github.com/jamescarr/nodejs-mongodb-streaming
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/iTkwCnyAiiQJ.

Bralima Branding

unread,
May 14, 2012, 12:14:56 PM5/14/12
to mongodb-user
Thank you very elliot, i used your second link and i can download file
now, mixing mongodb gridfs and node js fs modules.
But, So far, I can only download .txt files, I have a big problem with
others extensions like .docx, .pdf, ijpg etc..

Here is an example of downloading from my code, thank for your help

var fs = require("fs")
var Mongolian = require("mongolian")
var server = new Mongolian
var db = server.db("test")
var files = db.collection("fs.files")
var mime = require("mime")

var gridfs = db.gridfs()

// Create new file write stream
var stream = gridfs.findOne('test.jpg', function(err, file) {
if (err) throw err
var stream = file.readStream()
var content = ''
stream.on('data',function(chunk) { content +=
chunk.toString() })
stream.on('error', function(error) {
console.log(error)
})
stream.on('end',function() {
// console.log(content)
fs.writeFile('fs/test.jpg', content, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});

});

});

In this code i trie to download the file test.jpg from my mongodb
database to my fs folder... the file is created, but it's empty. I
think it's a encoding problem or something.. how to solve it ??? It's
the same with pdf, docx, odt files etc...


Thanks

Christian Kvalheim

unread,
May 15, 2012, 2:19:29 PM5/15/12
to mongodb-user
don't use mongolian. have a look at the example

http://mongodb.github.com/node-mongodb-native/api-generated/gridstore.html#read

should be fairly easy to understand

also have a look at the docs in general

http://mongodb.github.com/node-mongodb-native

Best of luck

Cheers

Bralima Branding

unread,
May 15, 2012, 6:54:37 PM5/15/12
to mongodb-user
Thank you very much christiam, it's working correctly now..

cheers

On May 15, 8:19 pm, Christian Kvalheim <chris...@gmail.com> wrote:
> don't use mongolian. have a look at the example
>
> http://mongodb.github.com/node-mongodb-native/api-generated/gridstore...
Reply all
Reply to author
Forward
0 new messages