Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  108] [ 3]  / answers: 1 / hits: 17590  / 11 Years ago, tue, december 31, 2013, 12:00:00

I am trying to figure out how I can post an image directly to GridFS without storing it anywhere on the server as a temporary file first.



I am using Postman (chrome ext.) to post a file, and I manage to store this post as a file using:



req.pipe(fs.createWriteStream('./test.png'));


I am also able to store directly to GridFS from a readStream when the readStream is created from a file on the server. (see code)



I have the following files, saveFromReq.js which listens for the POST and basically just passes this on to the savePic.js.



saveFromReq.js:



var express = require('express');
var app = express();
var savePic = require('./savePic');
var fs = require('fs');
var GridStore = require('mongodb').GridStore;
var pic = './square.png';
var picID;



//When the following

//var pic = fs.createReadStream('./square.png', {autoClose: true});

//is not commented out, and 'req' is replaced with 'pic' in the savePic function,
//the file square.png is stored correctly to GridFS

app.post('/picture', function(req, res){

savePic(req, function(id){});
res.writeHead(200, {'Content-Type': 'text' });
res.end(Sucsess!n);

});

app.listen(process.env.PORT || 3413);


savePic.js:



var savePic = function(req, callback){


var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
fs = require('fs');

//When the following

//req.pipe(fs.createWriteStream('./test.png'));

//is not commented out, the correct image is stored to test.png, and
//the sequence after req.on(data... starts
//(That sequence does not start at all when this is commented out..)

var fileId = new ObjectID();
var db = new Db('testDB', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {


var gridStore = new GridStore(db, 'test', 'w');

//open
gridStore.open(function(err, gridStore) {
console.log(opened);


req.on(data, function (data) {
console.log(data recieved);
gridStore.write(data, function (err, gridStore) {
if (err) {
console.log(error writing file);
}
});
});
req.on(end, function () {
gridStore.close(function (err, gridStore) {
if (!err) {
console.log(The file has been stored to database.);
db.close();
}
});
});
req.pipe(gridStore);


});




});
callback(fileId);
};
module.exports = savePic;


Any help would be greatly appreciated!


More From » node.js

 Answers
48

gridfs-stream makes that pretty easy:



// `gfs` is a gridfs-stream instance
app.post('/picture', function(req, res) {
req.pipe(gfs.createWriteStream({
filename: 'test'
}));
res.send(Success!);
});

[#73464] Monday, December 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madalynn

Total Points: 342
Total Questions: 95
Total Answers: 106

Location: Turkmenistan
Member since Sat, Apr 16, 2022
2 Years ago
;