I tried to file upload with NodeJS + ExpressJS + Multer but does not work well.
My ExpressJS Version is 4.12.3
this is my source
server.js:
var express = require('express'),
multer = require('multer');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(multer({ dest: './uploads/'}));
app.post('/', function(req, res){
console.log(req.body); // form fields
console.log(req.files); // form files
res.status(204).end()
});
app.get('/', function(req, res) {
res.sendFile('public/index.html');
});
app.listen(5000, function() {
console.log("start 5000");
});
public/index.html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input id="file" type="file"/>
<button type="submit">test</button>
</form>
</body>
</html>
My NodeJS Console Log when I click Submit Button:
"C:\Program Files\nodejs\node.exe" server.js
start 5000
{}
on NodeJS Console, there is empty object at req.files Is some problem on my source?
Use this example, it uses firebase cloud function and firebase storage to upload file but you can use it for anything , it uses express-multipart-file-parser and works like charm the above link just works perfectly,I tested this code and pushed it to repo for you guys.Just add your configuration and there you go.
Try this
ref: http://wiki.workassis.com/nodejs-express-get-post-multipart-request-handling-example/
I don't see you calling any API to upload file on click of submit button. Let me give you more comprehensive implementation.
multer config in
app.js
View
End point '
/user/profile_pic_upload
' POST callsuploadProfilePic
in controllerUpload profile pic logic in users controller