2
votes

My Node.js code:

app.use(fileUpload());

app.post('/upload', function(req, res) {
  if (!req.files)
    return res.status(400).send('No files were uploaded.');

  // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file
  console.log(req.files);
  let sampleFile = req.files[0];

  // Use the mv() method to place the file somewhere on your server
  sampleFile.mv('C:/Users/MNaaraayanan/Downloads/boilerplate_new/src/main/ngapp/', function(err) {
    if (err)
      return res.status(500).send(err);

    res.send('File uploaded!');
  });
});

The console.log():

{ 'uploads[]':
   { name: 'blank_user.png',
     data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 fb 00 00 00 fb 08 02 00 00 00 23 10 75 f1 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 ... >,
     encoding: '7bit',
     truncated: false,
     mimetype: 'image/png',
     md5: '0c803e9cef05aedeada6fb9587f1b073',
     mv: [Function: mv] } }

TypeError: Cannot read property 'mv' of undefined at C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\server.js:19:14 at Layer.handle [as handle_request] (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\layer.js:95:5) at C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:335:12) at Busboy.next (C:\Users\MNaaraayanan\Downloads\boilerplate_new\src\main\ngapp\node_modules\express\lib\router\index.js:275:10) at emitNone (events.js:111:20) at Busboy.emit (events.js:208:7)

the req.file contains the image but when I read the the .mv() function it is saying undefined.

1

1 Answers

5
votes

following your console.log, I think the structure should be

let sampleFile = req.files['uploads[]'];
sampleFile.mv();

it looks like req.files = { 'uploads[]': { mv } } , not req.files = [{ mv }]