My HTML file is like following,
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>file</title>
</head>
<body>
<form method="post" action="/getusrfile" enctype='multipart/form-data'>
<input type="file" name="fil" >
<button type="submit">
sub
</button>
</form>
</body>
</html>
I'm using node, express as following
app.post("/getusrfile",function(req,res){
console.log("came to server");// prints this
console.log(req.files);//prints undefined
//console.log(JSON.stringify(req.files.fil));
fs.readFile(req.files.fil, function (err, data) {
// ...
var newPath = __dirname + "/../public/uploadedFileName";
fs.writeFile(newPath, data, function (err) {
console.log(err);
res.send(err);
});
});
});
It prints came to server but no file/directory is created on the server. And the response is "Cannot POST /getusrfile" with status 404.
and req.files prints undefined
How to make it work?
First
Then add multer middleware before app.post function :
Test :