Nik Nik - 6 months ago 24
JSON Question

File not uploading properly

EDIT: Edited it using Mikhail's suggestion. Got closer to the solution

Hi I am trying to upload a JSON file using nodejs but for some reason it says my file is undefined. A file appears in my Public folder that contains the contents of the uploaded file however. I was wondering if someone would be able to help me out. Thanks

Here is the HTML

<form method="post" enctype="multipart/form-data" action="/file-upload">
<input type="file" name="theFile" >
<input type="submit" class = "submit">

EDIT: Here is the server.js'/testtwilio',upload.single('theFile'),function(req, res, next) {
console.log('FIRST TEST: ' + req.file);


Here is the JSON file

[{"name":"FIRST LAST","date":"12/22/2016","number":"7523924324"}]

Here is what is being logged

FIRST TEST: [object Object]


Change your JSON.stringify(req.files) to JSON.stringify(req.file)

Full code


<form id = "uploadForm" enctype = "multipart/form-data" action = "/api/file" method = "post">
    <input type="file" name="userFile" />
    <input type="submit" value="Upload File" name="submit">


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

      res.sendFile(__dirname + "/index.html");
});'/api/file', upload.single('userFile'), function (req, res, next) {

    console.log("Working on port 3000");

More about upload file with multer you can read in this example on StackOverflow Documentation Single File Upload using multer


File name which you use in multer.single() method should match name in input <input type="file" name="userFile" />