首页 文章

如何用fs.unlink删除本地文件?

提问于
浏览
4

CODE:

fs.unlink("/public/images/uploads/"+req.file.filename, (err) => {
        if (err) {
            console.log("failed to delete local image:"+err);
        } else {
            console.log('successfully deleted local image');                                
        }
});

ERROR MESSAGE IN CONSOLE/TERMINAL:

failed to delete local image:Error: ENOENT: no such file or directory, unlink '/public/images/uploads/ed6d810405e42d0dfd03d7668e356db3'

SITUATION

我必须指定错误的路径 . 我不明白为什么会出错,“ public " folder is at the same level as the " app.js " file. The " upload.js " is in a folder called " routes " which is at the same level as " app.js ” .

我已经在我的app.js中为我的公共文件夹指定了一个“/ public”路由:

//Static Folder
app.use("/public",express.static(path.join(__dirname, "/public")));

QUESTION:

我做错了什么?

1 回答

  • 16

    我打赌你想删除项目目录中的文件 . 试试这个(在“/”之前点):

    fs.unlink("./public/images/uploads/"+req.file.filename, (err) => {
            if (err) {
                console.log("failed to delete local image:"+err);
            } else {
                console.log('successfully deleted local image');                                
            }
    });
    

相关问题