node js multer file upload not working. req.file and req.files always undefined

Katie

I am trying to upload a file to my server, but req.file and req.files is always undefined on my POST REST endpoint.

The content I'm trying to upload is a ".dat" file, and I am expecting a json response.

Here's my code:

Server side:

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

var app = express()

app.post('/creoUpload', upload.single('uploadFileField'), function(req, res, next) {
  console.log("req.file = ",JSON.stringify(req.file)); //is undefined
  console.log("req.files = ",JSON.stringify(req.files)); //also undefined
});

Client Side:

<form id="creoUploadForm" action="/creoUpload" enctype="multipart/form-data" method="post">
    <input type='file' name='uploadFileField'><br><br>
    <input type='submit' value='Upload'/>
</form>

JS:

$( "#creoUploadForm" ).submit(function( event ) {
    event.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
          url: '/creoUpload',
          type: 'POST',
          data: formData,
          async: true,
          cache: false,
          contentType: false,
          processData: false,
          success: function (returndata) {
              console.log("RETURN DATA IS: ",returndata);
          },
          error: function (err) {
              console.log("ERROR: ",err);
          }
    });
});

I keep playing around with the fields but it's not working.. anyone see what I'm doing wrong here?

I'm following the example from https://www.npmjs.com/package/multer

Versions:

  • Express Version: 4.12.0
  • Node Version: 6.5.0
  • JQuery: 1.12.1

Thank you!

Sagar

You are using multer as a middleware, So before entering into your function and printing it has uploaded images to storage and removes record from req.files.

There are two ways you can access req.files.

  1. Use multer as a function stated by finw3.

  2. Another solution is:

//CODE STARTS

var storage = multer.diskStorage({

  destination: function (req, file, cb) {

    cb(null, '/filepath')
  },


  filename: function (req, file, cb) {

    let filename = 'filenametogive';
     req.body.file = filename

    cb(null, filename)
  }
})

var upload = multer({ storage: storage })

//CODE ENDS

Now you can access files in req.body.file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Node js (multer) req.file and req.files always undefined in react js but working fine with templating engines

multer - req.file always undefined

req.file is undefined (multer, node.js)

Multer middlware in Node.js returns empty object in req.body and undefined in req.file

Ionic native file transfer - File upload - Node js express server but req.files undefined

multer - req.file is undefined

multer req.file is undefined

Express file upload: req.files is undefined

req.file is undefined in multer image upload -- NodeJS, Angular

Multer req.file is undefined - using with Node/Express for image upload to s3

req.files is undefined when uploading file with multer

req.file is undefined (react.js and multer)

multer - req.file is undefined.

req.file is undefined - multer and nodejs

Multer gives req.file undefined

Multer req.file returns undefined

req.file is undefined using multer

Multer nodejs - req.file is undefined

Multer is uploading file, but not populating req.files

Why is req.file "undefined" for me when trying to upload a file using multer?

req.file is undefined when I upload image using multer and html input form

nodejs : multer - req.file is undefined when using upload.single() in a middleware

multer.single dosen't upload inout file to req

Uploading file with multer in nodejs returns req.file undefined and empty req.body

using multer to pass memoryStorage to express with ajax but req.file is undefined

Vue Frontend - Multer Express - Req.File is undefined

multer.memoryStorage() is returning req.file as undefined

Node.js and Multer - Handle the destination of the uploaded file in callback function (req,res)

MULTER - req.files.filename returns undefined