I am using dropzone to upload files from the UI. Here is my drozone configuration:
var imageUpload = new Dropzone('div#dataSection', {
url: 'api/image',
autoProcessQueue: true,
paramName: 'file'
});
I am using node and expressjs to build the server side endpoint based on this link
I am excuting below code on the server side.
fs.readFile(req.files.displayImage.path, function (err, data) {
// ...
var newPath = __dirname + "/uploads/uploadedFileName";
fs.writeFile(newPath, data, function (err) {
res.redirect("back");
});
});
The problem is req.files in coming as undefined. Am I missing anything?
The tutorial you were following is quite outdated, and certainly written for old version of
express
. Withexpress
4.0, released in April 2014, (almost) all built-in middleware was dropped, including the one parsing uploaded files asreq.files
.That's the problem, and the solution is to use separate module to handle uploaded files.
Multer
seems like a natural choice for that (kudos to @Ben Fortune for this suggestion).First, install
multer
Node.js module:Then, in the code on server side:
After that, all
POST
-ed files should be available asreq.files
in your route handler functions, in the format of Multer file objects.