express-fileupload: mv function not working but also not giving error
Hi, I am uploading image file. I am getting file in my server side. but when i execute ‘mv’ function it’s not uploading my file to given destination and its also not giving me any error.
my server side code.
var pic = req.files.profile_pic;
var fileName = user_id + '.jpg';
var newpath = "../public/images/" + fileName;
pic.mv(newpath,function (err) {
if(err){
console.log(err);
}
else{
console.log("File uploaded");
}
})
it prints file uploaded but file is not on given location ‘newpath’;
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (2 by maintainers)
“var newpath = “…/public/images/” + fileName;” I think your problem is here, fileupload doesn’t seem to use relative paths, try and use an absolute path to the file you want to save the images in. something like ./public/images/" + fileName;
the difference is the . at the beginning. … - relative path . - absolute path.
Hope that helps mate, it worked for me.
An enhancement that could help alleviate this issue is to add some sort of “create the directory if doesn’t exist” logic to
mv(). This could be an argument passed into themv()method itself, or as a globalexpress-fileuploadoption.Need to think about it some more, but I’ll keep this ticket open to serve as a reminder for this potential enhancement.
@24thdiv I think I found my problem, turns out express-fileupload will not create a new folder, you need to create the folder you want to move the file to.