Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The file object doesn't contain much info #333

Closed
mercmobily opened this issue Mar 30, 2016 · 7 comments
Closed

The file object doesn't contain much info #333

mercmobily opened this issue Mar 30, 2016 · 7 comments

Comments

@mercmobily
Copy link

I am using the DiskStorage backend.

My file object in all of the important callbacks (filter, destination, etc.) only has the following properties:

{ fieldname: 'fileName',
  originalname: 'test.mp4',
  encoding: '7bit',
  mimetype: 'video/mp4' }

In your documentation, you state:


Each file contains the following information:

Key Description Note
fieldname Field name specified in the form
originalname Name of the file on the user's computer
encoding Encoding type of the file
mimetype Mime type of the file
size Size of the file in bytes
destination The folder to which the file has been saved DiskStorage
filename The name of the file within the destination DiskStorage
path The full path to the uploaded file DiskStorage
buffer A Buffer of the entire file `MemoryStorag

Am I doing something wrong?

@LinusU
Copy link
Member

LinusU commented Mar 30, 2016

Could you please share the code that you are using?

@ekeitho
Copy link

ekeitho commented Apr 4, 2016

I am having a similar issue.
In the request body I send the file's name, however inside of the 'filename' function the request body is empty and the file only has 4 keys.
One of my thoughts were to try to edit the 'file' object that's passed into 'filename', inside of my app.post(), but not sure how I'd do that right now.

So far I found what line the file is being created.

var storage = multer.diskStorage({
     destination: function (req, file, cb) {
       cb(null, './uploads/')
     },
     filename: function (req, file, cb) {
       cb(null, "" + '-' + Date.now())
       console.log(req.body);
       console.log(file);
       /*
          { }
          { fieldname: 'image',
            originalname: 'ekeitho.jpg',
            encoding: '7bit',
            mimetype: 'image/jpeg' } */
      }
   });
var upload = multer({dest: './uploads/', storage: storage});

app.post('/pictures', upload.single('image'), function(req,res) {
   req.file.filename = req.body.filename;
});

@LinusU
Copy link
Member

LinusU commented Apr 5, 2016

Okay, I reread the first question again and realised that I missed where it was the first time.

@mercmobily That is expected behaviour since we can't know the size of the file before it has been uploaded. We also can't know the destination before the destination has been set, what do you think that destination, filename and path should be? Before the function that populates those values have been run we can't possible set them to anything useful?

@ekeitho You have a totally different problem that is mentioned in the documentation. This has come up a number of times before, e.g. #146 where the workaround is outlined...

Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.

In addition to that, you can't just change the filename property and expect the file to be moved on the disk. If you want to rename it in your route hander, you'll have to use fs.rename or something similar...

@LinusU LinusU closed this as completed Apr 5, 2016
@mercmobily
Copy link
Author

My apologies for not responding quicker.
So... When is file fully populated? In which imstance?
On 5 Apr 2016 1:37 pm, "Linus Unnebäck" notifications@github.com wrote:

Okay, I reread the first question again and realised that I missed where
it was the first time.

@mercmobily https://github.com/mercmobily That is expected behaviour
since we can't know the size of the file before it has been uploaded. We
also can't know the destination before the destination has been set, what
do you think that destination, filename and path should be? Before the
function that populates those values have been run we can't possible set
them to anything useful?

@ekeitho https://github.com/ekeitho You have a totally different
problem that is mentioned in the documentation. This has come up a number
of times before, e.g. #146
#146 where the workaround is
outlined...

Note that req.body might not have been fully populated yet. It depends on
the order that the client transmits fields and files to the server.

In addition to that, you can't just change the filename property and
expect the file to be moved on the disk. If you want to rename it in your
route hander, you'll have to use fs.rename or something similar...


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#333 (comment)

@LinusU
Copy link
Member

LinusU commented Apr 5, 2016

When control is handed over to your route handler

const upload = multer(/* ... */).single('file')

app.post('/test', upload, function (req, res, next) {
  // Here everything will be populated
})

@mercmobily
Copy link
Author

I have this in my server.js:

var multer = require('multer')
var upload = multer( { dest: '/tmp/uploads' }

...
app.post('/video/submit', upload.single('video'), function( req, res, next
) {

// Needs to be logged in for this to work
if( ! req.session.userId ) return next( new e.UnauthorizedError() );

// Here, 'file' is not populated
console.log( require('util').inspect( req.file, { depth: 10 } ) );
});

I can send you a minimal server.js to show it, but that's what's happening
now...

Merc.

On 5 April 2016 at 16:06, Linus Unnebäck notifications@github.com wrote:

When control is handed over to your route handler

app.post('/test', uploade, function (req, res, next) {
// Here everything will be populated
})


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#333 (comment)

@mercmobily
Copy link
Author

(It also happens with upload.any() BTW!

On 6 April 2016 at 10:50, Tony Mobily merc@mobily1.com wrote:

I have this in my server.js:

var multer = require('multer')
var upload = multer( { dest: '/tmp/uploads' }

...
app.post('/video/submit', upload.single('video'), function( req, res, next
) {

// Needs to be logged in for this to work
if( ! req.session.userId ) return next( new e.UnauthorizedError() );

// Here, 'file' is not populated
console.log( require('util').inspect( req.file, { depth: 10 } ) );
});

I can send you a minimal server.js to show it, but that's what's happening
now...

Merc.

On 5 April 2016 at 16:06, Linus Unnebäck notifications@github.com wrote:

When control is handed over to your route handler

app.post('/test', uploade, function (req, res, next) {
// Here everything will be populated
})


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#333 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants