aheckmann / gridform

Stream formidable uploads into MongoDB GridFS
http://aheckmann.github.com/gridform/
MIT License
59 stars 17 forks source link

File id is not available on 'fileBegin', 'end', 'file', or while parsing #10

Open TonsOfFun opened 10 years ago

TonsOfFun commented 10 years ago

I have successfully setup a simple express app utilizing gridform for file uploads to GridFS. The problem I have is I would like to use the file object id for logging/enqueueing.

router.post('/files', function(req, res) {
      // Assume you have gridform setup
      var form = gridform();

      form.on('fileBegin', function (name, file) {
        console.log('on fileBegin\n');
        console.log(file);
        console.log('\n');
      });

      form.parse(req, function (err, fields, files) {
        console.log('while parsing\n');
        console.log(files.file);
        console.log('\n');
      });

      form.on('file', function (name, file) {
        console.log('on file\n');
        console.log(file);
        console.log('\n');
      });

      form.on('end', function (name, file) {
        console.log('on end:\n');
        console.log(file);
        console.log('\n');
      });
});

Gives me this

on fileBegin:

{ domain: null,
  _events: {},
  _maxListeners: 10,
  name: 'IMG_4073.jpg',
  path: 'IMG_4073.jpg',
  type: 'image/jpeg',
  size: 0,
  root: null,
  id: null,
  lastModified: Sat May 24 2014 16:28:33 GMT-0700 (PDT) }

on file:

{ domain: null,
  _events: {},
  _maxListeners: 10,
  name: 'IMG_4073.jpg',
  path: 'IMG_4073.jpg',
  type: 'image/jpeg',
  size: undefined,
  root: undefined,
  id: undefined,
  lastModified: Sat May 24 2014 16:28:33 GMT-0700 (PDT) }

while parsing:

{ domain: null,
  _events: {},
  _maxListeners: 10,
  name: 'IMG_4073.jpg',
  path: 'IMG_4073.jpg',
  type: 'image/jpeg',
  size: undefined,
  root: undefined,
  id: undefined,
  lastModified: Sat May 24 2014 16:28:33 GMT-0700 (PDT) }

on end:

undefined

I realize on end doesn't return the file, but I was desperate. Is there something I am missing?

isarathg commented 10 years ago

+1

Logic-Seeker commented 10 years ago

+1

TonsOfFun commented 10 years ago

This seems to do the trick of getting me the id when I want it, though it feels like an unnecessary hack. Furthermore I did notice while playing with gridform's tests I can access the id inside the parse block. This indicates to me there is something wrong with the copy I am using in my application or with my application's implementation or configuration. Any advise would be greatly appreciated.

:beers: Cheers!