1

I'm trying to upload a file in meteor using HTTP POST method and enctype="multipart/form-data"

WebApp.connectHandlers.use("/api/v1/upload", function(req, res, next)   {
  console.log(req.files); //undefined
  console.log(req.file); //undefined
  console.log(req);
})

I tried with WebApp but getting undefined file property under request object

I also tried with multer and Picker but no luck.

const _multerInstanceConfig = { dest: '/tmp' }; // Temp dir for multer
const _multerInstance = multer(_multerInstanceConfig);

Picker.middleware(_multerInstance.single('photo'));
Picker.route('/api/v1/upload', function(params, req, res, next) {
  console.log(req.files); //undefined
  console.log(req.file); //undefined
  console.log(req);
})

This is simplest form I'm trying to upload is

<form action="http://localhost:3000/api/v1/upload" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Upload File" />
</form>

Am I missing something here? not sure. Also, I don't want to upload files using base64 data string via DDP as it very slow.

I've check couple of link also with no luck
multer - req.file always undefined
https://github.com/noris666/Meteor-Files-POST-Example

PS: I need to upload images via native Android/iOS clients.

Community
  • 1
  • 1
Ankit Balyan
  • 1,319
  • 19
  • 31

1 Answers1

1

Thanks, everyone, who has spent their time on my question, I got the solution for the problem from git issue, I raised. I'm posting here my solution if someone else also faces the similar problem.

It was because of the name of this input field

<input type="file" name="file" />

which doesn't match with

Picker.middleware(_multerInstance.single('photo'));

changing either of them to will make it work perfectly.

Ankit Balyan
  • 1,319
  • 19
  • 31