Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i delete a file from my nodejs server

I have uploaded files into a temp directory using multer

const imageStorage = multer.diskStorage({
    // Destination to store image     
    destination: 'images', 
      filename: (req, file, cb) => {
          cb(null, file.fieldname + '_' + Date.now() 
             + path.extname(file.originalname))
             fs.writeFile("/tmp/"+file.originalname, message, function (err) {
        if (err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });
    }
});

How can i remove it ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can delete the file using

 fs.unlink(path, (err) => {
  if (err) throw err //handle your error the way you want to;
  console.log('path/file.txt was deleted');//or else the file will be deleted
    });
  );

Refernce : https://nodejs.org/api/fs.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading