I am using multer to upload files via express. Is there a way to perform a database operation before uploading the file? I need to perform a conditional check whether the user insertion in the database was successful or not depending on that, the file upload should be performed.
Asked
Active
Viewed 2,034 times
1 Answers
0
To use multer
as per required you can use the following code:
var path = require('path');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, './uploads')
},
filename: function(req, file, callback) {
console.log(file)
callback(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
}
});
destination
is to specify the path to the directory where uploaded files will be stored.
filename
is used to determine what the file should be named inside the folder. In this case, we upload the file using a return number value from Date.now() instead of the original name and add a original file extension using the built-in path Node.js library.
path.extname(file.originalname);
app.post('/insertUpload', function(req, res) {
// connection is db connection variable
var username = req.body.username;
var query = connection.query('INSERT INTO users SET ?', username,
function(err, result) {
console.log(result);
if(!err){ // if not error after insert
var upload = multer({
storage: storage
}).single('userFile')
upload(req, res, function(err) {
res.end('File is uploaded')
});
}
});
});

Deep Kakkar
- 5,831
- 4
- 39
- 75
-
In this line `var query = connection.query('INSERT INTO users SET ?', username` I want to access username from `req.body` as it will be coming from the user – Ryan Dsouza Nov 24 '17 at 06:48
-
it is just query to insert data into table.. I believe you will replace this with your db query – Deep Kakkar Nov 24 '17 at 06:50
-
I have edited answer, please check. now using req.body.username – Deep Kakkar Nov 24 '17 at 06:52
-
req.body is is always **undefined** before `upload(req, res, function(err) { res.end('File is uploaded') })` – Ryan Dsouza Nov 25 '17 at 07:20
-
there can be various reasons for req.body as undefined . check this answer https://stackoverflow.com/questions/9177049/express-js-req-body-undefined – Deep Kakkar Nov 27 '17 at 06:55
-
`bodyParser` cannot parse multipart data. That's the reason req.body is undefined – Ryan Dsouza Nov 27 '17 at 11:57
-
No. Before multer upload call, the req.body is always undefined – Ryan Dsouza Nov 27 '17 at 12:30
-
Require body-parser (to receive post data from clients) – Deep Kakkar Nov 27 '17 at 13:33
-
var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) – Deep Kakkar Nov 27 '17 at 13:33
-
1`body-parser` is unable to handle multipart form data. – Ryan Dsouza Nov 29 '17 at 10:02
-
@RyanDsouza what we have to do ? – siva Aug 23 '18 at 12:12
-
@RyanDsouza I prefer multer to handle multipart data. – Deep Kakkar Dec 03 '19 at 12:40