0

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.

8
  • can be file write permission issue in your server Commented Jun 24, 2016 at 6:39
  • TypeError: fs.move is not a function I am getting. How to check the permission issue. Commented Jun 24, 2016 at 6:47
  • I have updated my question with error.Please have a look Commented Jun 24, 2016 at 6:53
  • @ Subburaj File permission is 777 Commented Jun 24, 2016 at 6:53
  • 1
    http://example.com/images/user-profile.jpg is 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... Commented Jun 24, 2016 at 8:12

2 Answers 2

2

You cannot write the image you are receiving as an url ...
It should be a local path on the server that is running your application.

fs.readFile(req.files.displayImage.path, function (err, data) {
  // ...
  var newPath = __dirname + "/uploads/uploadedFileName";
  fs.writeFile(newPath, data, function (err) {
    res.redirect("back");
  });
});

Also do not re-write the image into its new path, but instead move it (with rename()):

fs.readFile(req.files.displayImage.path, function (err, data) {
  // ...
  var newPath = __dirname + "/uploads/uploadedFileName";

  fs.rename(files.upload.path, _path +'/'+ img_name + '.png', function (error) {
    res.redirect("back");
  });
});
Sign up to request clarification or add additional context in comments.

Comments

-1

use this in the app.js

app.use('/uploads', express.static('uploads'));

1 Comment

This is useful for serving static files, but the question is about file uploads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.