如何解决使用取消链接从磁盘删除文件时如何指定正确的路径?
我想删除保存在我的项目应用程序中的项目目录中的图像。为此,我使用了fs.unlink()
。
Photo.findByIdAndDelete(req.body.photoId)
.exec((error,photo) => {
// save image path to variable
const fullImagePath = photo.photo.photoUrl;
// regex that match any url
const urlRegex = /(http|https):\/*[a-z]*(.|:)[a-z0-9]*(.(com|co.uk|en|ro))?/g;
// the url trimmed from image path
const trimmedImagePath = fullImagePath.replace(urlRegex,'');
// delete the image from the specified location on disk
fs.unlink(path.join(__dirname,trimmedImagePath),error => {
if (error) throw error;
console.log('File deleted');
});
});
});
但是我没有删除图像,而是得到了这个错误:
[start:server] [Error: ENOENT: no such file or directory,unlink 'F:\dev\MY_PROJECT\server\controllers\photos\uploads\1605211105520_bg.jpg'] {
[start:server] errno: -4058,[start:server] code: 'ENOENT',[start:server] syscall: 'unlink',[start:server] path: 'F:\\dev\\MY_PROJECT\\server\\controllers\\photos\\uploads\\1605211105520_bg.jpg'
[start:server] }
要保存图像的文件夹位于服务器文件夹中的/ uploads中,用于删除图像的控制器位于controllers / photos文件夹中
MY_PROJECT
client
server
controllers
photos
**delete-photo.js**
models
routes
**uploads**
因此,从错误中可以清楚地看出,在unlink方法中图像位置不正确。
如何在unlink方法中指定正确的文件夹,以便删除图像?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。