gpt4 book ai didi

javascript - 找不到已删除的图像

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:44 25 4
gpt4 key购买 nike

我在node JS服务器上编写的函数不会删除图像。他找不到那张照片。但是,当您键入图片名称时,它会将其从文件夹中删除。

我收到此错误:

无法删除本地镜像:错误:ENOENT:没有这样的文件或目录,取消链接“C:\Users\mary\Desktop\Node.js-UploadFiles-RestAPIs\uploads\undefined”

NodeJS:文件 Controller


const uploadFolder = __basedir + '/uploads/';
const fs = require('fs');


exports.deleteFile=(req, res)=> {

let filename = req.params.filename;
fs.unlink(uploadFolder + filename, (err) => {
if (err) {
console.log("failed to delete local image:"+err);
} else {
console.log('successfully deleted local image');
}
});

}

NodeJS:文件路由器

let express = require('express');
let router = express.Router();


let fileWorker = require('../controllers/file.controller.js');

router.delete('/api/file/delete', fileWorker.deleteFile);

module.exports = router;

服务.ts:


delete(file : File):Observable<HttpEvent<{}>>{
// const formdata: FormData = new FormData();
// formdata.append('file' , file);
const req = new HttpRequest('DELETE', 'http://localhost:8000/api/file/delete',file, {
responseType: 'text'

});
return this.http.request(req);
}

组件.ts:

  x:any[];

deleteImage(y){

// @ts-ignore
this.x=getFile();

this.imageServis.delete(y).subscribe(event => {

console.log("Here");

});
}

最佳答案

查看错误中的未定义:

'C:\Users\mary\Desktop\Node.js-UploadFiles-RestAPIs\uploads\undefined'

文件名未在您的函数中定义:

exports.deleteFile=(req, res)=> {

let filename = req.params.filename;
fs.unlink(uploadFolder + filename, (err) => {

您使用 req.params 但似乎您在正文中发送了文件名:

 const req = new HttpRequest('DELETE', 'http://localhost:8000/api/file/delete',file, {
responseType: 'text'

});

如果您想使用 req.params,请执行以下操作:

 const req = new HttpRequest('DELETE', 'http://localhost:8000/api/file/delete'+file,null, {
responseType: 'text'

});


router.delete('/api/file/delete/:filename', fileWorker.deleteFile);

关于javascript - 找不到已删除的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com