找不到已删除的图像

时间:2019-04-08 12:09:51

标签: javascript node.js api

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

我收到此错误:

无法删除本地映像:错误:ENOENT:没有此类文件或目录,请取消链接'C:\ Users \ mary \ Desktop \ Node.js-UploadFiles-RestAPIs \ uploads \ undefined'

NodeJS:文件控制器


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; 

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

component.ts:

  x:any[];

  deleteImage(y){

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

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

      console.log("Here");

    });
}

1 个答案:

答案 0 :(得分:0)

查看错误中的未定义内容:

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