【问题标题】:How can i delete a file from my nodejs server如何从我的 nodejs 服务器中删除文件
【发布时间】:2022-01-10 14:34:15
【问题描述】:

我已使用 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!");
    });
    }
});

我怎样才能删除它?

【问题讨论】:

    标签: node.js express file


    【解决方案1】:

    您可以使用删除文件

     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
        });
      );
    

    参考:https://nodejs.org/api/fs.html

    【讨论】:

      猜你喜欢
      • 2013-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2011-10-11
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多