2016-12-31 14 views
2

CODE:如何使用fs.unlink刪除本地文件?

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

錯誤消息CONSOLE/TERMINAL:

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

狀況

我必須指定錯誤的路徑。我不明白爲什麼它會出錯,「public」文件夾與「app.js」文件處於同一級別。 「upload.js」位於與「app.js」處於同一級別的文件夾「routes」中。

而且我指定的路線 「/公衆」 我的公用文件夾在我app.js:

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

問題:

我做了什麼錯?

+0

該文件是否存在?在錯誤描述中,它沒有擴展名。這不完全是一個問題,但有點不尋常 –

+0

@RômuloM.Farias是的,這是一個保存在二進制文件中的圖像。 – Coder1000

回答

13

我打賭你想刪除項目目錄中的文件。試試這個(在「/」之前的點):

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