Reputation: 13908
Here is my code:
exports.post_handler = function(req, res) {
var photo = req.files.image;
console.log(photo);
console.log(__dirname);
fs.readFile(photo.path, function(err, data) { //I use the path module to join the image path strings
fs.rename(path.join(__dirname, "public/temp"), path.join(__dirname,"public/images"), function(err) {
if (err) {
console.log(err);
res.redirect("/");
}
else {
console.log("file " + photo.name + "written to uploads folder");
res.redirect("/home");
}
});
});
}
I'm trying to move an uploaded image file from my temp
folder to my uploads
folder. I'm using the fs
module to do this. After granting full permissions to both files to all users on my PC, I'm getting the following error:
{ [Error: EPERM, rename 'dir\public\temp']
errno: 50,
code: 'EPERM',
path: 'dir\\public\\temp' }
I'm not sure what's going wrong here. Anyone have any ideas?
Upvotes: 4
Views: 4018
Reputation: 38616
What your code is trying to do is rename the public/temp
directory to public/images
. public/images
presumably already exists, so you're getting that error. In other words, nowhere in there are you moving the image, you're instead 'moving' (renaming) the directory public/temp
to public/images
.
You have to use photo.path
instead. Use it as the first parameter and then perhaps the second parameter should path.join
images directory to path.basename(photos.path)
.
Upvotes: 2