aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

How can you stream multiple files passed from a form. #60

Closed jsrosas closed 9 years ago

jsrosas commented 9 years ago

Hi I cannot figure out how to pass multiple files from one form. I am using multer. And the output from multer after processing the form is

  name: 'llelelel',
  bio: 'lelelelle',
  id: '36546987632',
  tel: '36546987632',
  company: 'lelelelle',
  dni: '36546987632' }
{ avatar: 
   { fieldname: 'avatar',
     originalname: 'mY9Lyku1.png',
     name: '828d9c51e7ab2e5d4b61dc840e32b723.png',
     encoding: '7bit',
     mimetype: 'image/png',
     path: '/home/ubuntu/workspace/public/uploads/828d9c51e7ab2e5d4b61dc840e32b723.png',
     extension: 'png',
     size: 336148,
     truncated: false,
     buffer: null },
  dnifront: 
   { fieldname: 'dnifront',
     originalname: 'images.jpg',
     name: '33d4472106e3d68fd3b58a13315b9ef5.jpg',
     encoding: '7bit',
     mimetype: 'image/jpeg',
     path: '/home/ubuntu/workspace/public/uploads/33d4472106e3d68fd3b58a13315b9ef5.jpg',
     extension: 'jpg',
     size: 10027,
     truncated: false,
     buffer: null },
  dniback: 
   { fieldname: 'dniback',
     originalname: 'DNI.png',
     name: '0c5fdcd66c6f5d993eeab95ce1386efa.png',
     encoding: '7bit',
     mimetype: 'image/png',
     path: '/home/ubuntu/workspace/public/uploads/0c5fdcd66c6f5d993eeab95ce1386efa.png',
     extension: 'png',
     size: 150021,
     truncated: false,
     buffer: null } }

Do i have to create a stream for each image? var writestream 1 then pipe that, then var wraitestream2 and s on?

Reggino commented 9 years ago

Hi, I don't know multer, but if you can provide a test for your issue, i'ld be happy to look into it.

jsrosas commented 9 years ago

Thanks a lot Reggiino. My code to recieve from form post is like this. But the form also contains 5 more images. RIght now I am saving each image by creating a new writestream and giving it the req.file.file.path. Is there a better solution? When I use something like mongovue it only shows one image then the rest of the images give me errors. But I do see them saved on mongo as files each with correct information and metadata. I am doing this with express, multer, mongoose. Is this the right way to do it? Like I mentioned mongovue can only display one image the rest of them give me errors.

    app.post('/new',function(req,res){
    console.log(req.body);
    console.log(req.files);
    var taxi = new taxiModel.Taxi({
                    firstname:req.body.firstname,
                    lastname:req.body.lastname,
                    tel:req.body.tel,
                    company:req.body.company,
                    dni:req.body.dni,
                    dnidate:req.body.dnidate,
                    license:req.body.license,
                    licensedate:req.body.licensedate
                             });
     taxi.save(function(err){
         console.log(err);
     var path1 = req.files.avatar.path;
     var gfs = Grid(conn.db);
//here i start doing the stream per each image.
     var writestreamAvatar = gfs.createWriteStream({
                                                filename : req.body.dni,
                                                //metadata : {type:'avatar'}
                                            });
fs.createReadStream(req.files.front.path).pipe(writestreamFront);
//this is the stream for the second img and so on.
var writestreamDnifront = gfs.createWriteStream({
                                                filename : req.body.dni,
                                                //metadata : {type:'dnifront'}
                                            });
    fs.createReadStream(req.files.dnifront.path).pipe(writestreamDnifront);
Reggino commented 9 years ago

Do i have to create a stream for each image? var writestream 1 then pipe that, then var wraitestream2 and s on?

Yes.

And as far as i can quickly see, this looks pretty correct. If you have any other errors or issues, please let us know.