Req.file object is always undefined, HTTP file upload

#1

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

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

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

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);
})

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.
PS: I need to upload images via native Android/iOS clients.

#2

I know this is almost 2 year ago but did you find a solution for this? I have the same problem now.

#3

Solved. Some middleware was needed to make multipart form data available on req, I used ‘multer’. A working solution: (modified a bit for this post)

import multer from 'multer'; // https://github.com/expressjs/multer
import connectRoute from 'connect-route'; // https://github.com/baryshev/connect-route

const memoryStorage = multer.memoryStorage();
WebApp.connectHandlers.use(multer({ storage: memoryStorage }).any());

WebApp.connectHandlers.use(connectRoute(function(router) {

  router.post('/uploadfile', function(req, res, next) {

    // Do something with req.files
    console.log(req.files);

  })

}));