0

I tried all possible scenarios and I tried with multiple variations and approaches suggested by documentation and by other stackoverflow questions.

Any of them worked -> I keep getting req.file is: undefined Form:

<form action="/send" enctype="multipart/form-data" method="POST">

        <input type="file" id="file" name="file">
        <button type="submit">Submit</button>

Express Setup:

const express = require('express');
const ejs = require('ejs');
const homeController = require('./controlers/homeController');
const bodyParser = require('body-parser');



const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static('public'));


app.listen(3000);


/* routes */
app.use(homeController);

I have the following code:

var multer = require('multer')

const storage = multer.diskStorage({
    destination: './public/data/uploads',
    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
    }
});

var upload = multer({
    storage: storage
}).single('file');

router.get('/', (req, res) => {
    res.render('home')
})


router.post('/send', (req, res) => {

    upload(req, res, (err) => {
        if (err) console.log(err);

        console.log('req.file is: ' + req.file);
    });

})

I spent 2 days trying to figure this out but I can't see the light at the end of the tunnel, and i just want to get the file from client and send it with nodemailer as attachement later on.

  • Can you show how you've setup express? Also does it also fail if you try the upload through curl/postman? – eol Aug 29 '20 at 17:16
  • @eol yes, postman also fails. I added the express setup. Is there any setup we need to do specifically for this? I replicated setup I have seen in tutorials, documentation etc etc and none of it works for me. – Paulo Brás Aug 29 '20 at 22:08
  • @Paulo When you test with postman, you pass your data including image in `form data` tab? – Htet Phyo Naing Aug 30 '20 at 07:37
  • @HtetPhyoNaing yes. Is it possible that because I'm using firebase it is blocking the data for some reason? I tried to create a brand new app without putting it in firebase and it works properly, so I am wondering if this is not firebase related. – Paulo Brás Aug 30 '20 at 11:07
  • Does this answer your question? [How to perform an HTTP file upload using express on Cloud Functions for Firebase (multer, busboy)](https://stackoverflow.com/questions/47242340/how-to-perform-an-http-file-upload-using-express-on-cloud-functions-for-firebase) – eol Aug 30 '20 at 13:21
  • 1
    @eol thank you! It seems I am not alone with this issue. It is good to know it wasn't due to my code. I will give it a try and provide a comment back with the solution that worked for me. – Paulo Brás Aug 31 '20 at 08:47
  • 1
    @eol managed to get solution... I'm so happy - thank you so much for pointing me to this answer. – Paulo Brás Aug 31 '20 at 18:16
  • Happy to help! :) – eol Aug 31 '20 at 18:33

2 Answers2

1

Answer obtained based on eMAD suggestion on question How to perform an HTTP file upload using express on Cloud Functions for Firebase (multer, busboy)

Solution below:

  • File is saved on the storage location you define in muller.
  • req.files is an array with the details of the file/files you upload.

PS: thanks @eol for pointing me in this direction

const contactControler = require('../controlers/contactController');
const busboy = require('busboy')
const express = require('express');
const router = express.Router();
var multer = require('multer');
const SIZE_LIMIT = 10 * 1024 * 1024 // 10MB
var stor = multer.diskStorage({
    destination: '../public/cv',
    filename: function(req, file, callback) {
        callback(null, file.originalname);
    }
});

const multipartFormDataParser = multer({
    storage: stor,
    // increase size limit if needed
    limits: { fieldSize: SIZE_LIMIT },
    // support firebase cloud functions
    // the multipart form-data request object is pre-processed by the cloud functions
    // currently the `multer` library doesn't natively support this behaviour
    // as such, a custom fork is maintained to enable this by adding `startProcessing`
    // https://github.com/emadalam/multer
    startProcessing(req, busboy) {
        req.rawBody ? busboy.end(req.rawBody) : req.pipe(busboy)
    },
})


router.post('/send', multipartFormDataParser.any(), contactControler.send);

0

Please try adding upload in your route as middleware.

router.post('/send', upload, (req, res) => { ...
Htet Phyo Naing
  • 464
  • 7
  • 20