Using multer with inmemory option and streaming into mongodb gridfs

user1859465 picture user1859465 · Jan 13, 2015 · Viewed 12.7k times · Source

The following priece of code works as expected: it reads a file that has been uploaded using multer [https://github.com/expressjs/multer] and streams it into gridfs. Im also able to steam this out of gridfs as well.

var target = gfs.createWriteStream({
            filename: fileItem.originalname,
            mode: 'w',
            metadata: metaData
        });

        fs.createReadStream(tempFile)
            .on('end', function () {
                console.log('File read and stored');
            })
            .on('error', function (err) {
                console.log('Error handling file ' + err);
            })
            .pipe(target);

        // Attach new file to document list
        target.on('close', function (file) {
            filesUploadedList.push(file._id);

fileItem is obtained from iterating over req.files.file which are the files uploaded.

However, I'm trying to figure out the inMemory option for multer. If I set this to true then the fileItem will have the buffer populated. Just to note without this option the buffer/contents of the fileItem is empty which is why in the code above its reading from the fileItem.location.

What is the best way to populate the target with contents? the fs.createReadStream is doing the piping at the moment.

Thanks.

J

Answer

user1859465 picture user1859465 · Jan 13, 2015

You can accomplish this by using streamifier. The code looks should look like this:

streamifier.createReadStream(fileItem.buffer).pipe(target);

Multer does not write anything to disk, target is now populated with a the buffer/contents of the file being uploaded.