I need to upload the image to server using node js.The image is coming from ios app as a file data. I have used the following code,
var fs = require("fs");
var imageName = req.files.profile_image.name;
fs.readFile(req.files.profile_image.path, function (err, data) {
console.log(imageName);
// If there's an error
if(!imageName){
console.log("There was an error")
//res.redirect("/");
//res.end();
} else {
console.log(data);
var newPath = 'http://example.com/images/' + imageName;
// write file to uploads/fullsize folder
fs.writeFile(newPath, data, function (err) {
// let's see it
console.log(err);
//res.redirect("http://example.com/images/" + imageName);
});
}
});
When I run this code, I have received no error but the image is not uploaded in images folder.
I am getting the following error with fs.writeFile,
{ Error: ENOENT: no such file or directory, open 'http://example.com/images/user-profile.jpg'
at Error (native)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'http://example.com/images/user-profile.jpg' }
Please help.
http://example.com/images/user-profile.jpgis not a valid file system path. Why are you trying to save the file with a hard-coded domain in url format? Is that a mistake or do you want/need it like that for some reason? Saving the file with that name won't make it available from that url...