I would like to return the randomly generated file name back so that the front end can store the filename in the db, but I can't figure out how to use the callback to return the filename to somewhere I can use res.send to send it out. Here's my code:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '../images/');
},
filename: function (req, file, cb) {
var id = util.random_string(16) + Date.now() + path.extname(file.originalname);
cb(null, id);
}
});
var upload = multer({ storage: storage }).single('image');
router.post('/image/', function (req, res) {
upload(req, res, function (err) {
if (err){
console.log(JSON.stringify(err));
res.status(400).send("fail saving image");
} else {
res.send('how can I return the file name?');
}
});
});
Look under "File information" in the documentation.
req.file.filename
@louistsaitszho Did you figured out, to return the filename to front end. If yes please tell me.
@MuhammadTalhaAfzal I'm having the same problem, i couldn't return filename to front end :( i'm using angular 2
same issue, could u pls notify me in case u find the solution
Hi Louis,
For people who are struggling with this issue.
The filename is in the res.req.file.filename variable.
In your code it would look like this:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '../images/');
},
filename: function (req, file, cb) {
var id = util.random_string(16) + Date.now() + path.extname(file.originalname);
cb(null, id);
}
});
var upload = multer({ storage: storage }).single('image');
router.post('/image/', function (req, res) {
upload(req, res, function (err) {
if (err){
console.log(JSON.stringify(err));
res.status(400).send('fail saving image');
} else {
console.log('The filename is ' + res.req.file.filename);
res.send(res.req.file.filename);
}
});
});
@LinusU
""""""this is my code but it is not saving any to image and passport_image that's i am not able to display it on the front-end """"""
var cpUpload = upload.fields([{ name: 'image' , maxCount: 1}, { name: 'passport_image' , maxCount: 1}]);
app.post("/new_profile",cpUpload, function(req, res){
var profile_name = req.body.profile_name;
var passport_number = req.body.passport_number;
var Address = req.body.Address;
var phone_number = req.body.phone_number;
var money_paid = req.body.money_paid;
var passport_status = req.body.passport_status;
var image = req.files.path;
var passport_image = req.files.path;
var newProfile = {
profile_name: profile_name, passport_number: passport_number,
Address: Address, phone_number: phone_number, money_paid: money_paid, image: image,passport_status:passport_status,passport_image:passport_image};
NewProfile.create(newProfile, function(err,new_Profile){
if(err){
console.log(err);
console.log(res.files);
}else{
console.log(req.files);
console.log(new_Profile);
res.redirect("/profiles");
};
});
});
@vanhooferwin thank you for the perfect solution
I'm uploading multiple images how can I get the names of uploaded images?
this is the error i got!
global.picloc[0] = res.req.file.filename;
^
TypeError: Cannot read property 'filename' of undefined
@kondashiva could you post the code you are using?
I'm uploading multiple images how can I get the names of uploaded images?
I am returning the new name of the uploading images by accesing the req from within the res...
var uploadBusinessImages = multer({ storage: businessImagesStorage }).array("file");
router.post("/business", (req, res) => {
uploadBusinessImages(req, res, function(err) {
if (err instanceof multer.MulterError) {
return res.status(500).json(err);
} else if (err) {
return res.status(500).json(err);
}
//Returning (res.req.files) -> Has an array with the multiple file information and their new names
return res.status(200).send(res.req.files);
});
});
this is the error i got!
global.picloc[0] = res.req.file.filename;
^TypeError: Cannot read property 'filename' of undefined
req.files[index].fieldname
Look under "File information" in the documentation.
req.file.filename
It is file.name
const file = req.files && req.files.file;
if (file && file.name) {
const filename = file.name;
Most helpful comment
Hi Louis,
For people who are struggling with this issue.
The filename is in the res.req.file.filename variable.
In your code it would look like this: