首页 > 解决方案 > 如何获取要从文件池中删除的图像名称反应

问题描述

我正在使用React Filepond将图像上传到使用 Multer 的 node.js 服务器。上传工作正常,但是当我尝试在上传后立即撤消(删除)上传时,我收到 404 错误。

    <FilePond
      ref={ref => (this.pond = ref)}
      files={this.state.files}
      allowMultiple={false}
      maxFiles={1}
      name="photo"
      server={{ url: "http://localhost:8000/api/uploads/", revert: "http://localhost:8000/api/revert/" }}
      oninit={() => this.handleInit()}
      onupdatefiles={fileItems => {
        // Set currently active file objects to this.state
        this.setState({
          files: fileItems.map(fileItem => fileItem.file)
        });
      }}
    />

这可能是因为它找不到文件名。在节点方面,我将文件名设置如下:

const multerStorage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, "public/img");
  },
  filename: (req, file, cb) => {
    const ext = file.mimetype.split("/")[1];
    cb(null, `hero-${Date.now()}.${ext}`);
  }
});

这给了我:

英雄1564944240035

现在,当我尝试删除它时,我得到了 404。如果我再次尝试创建该名称,时间戳将会不同。获得该确切文件名以使删除工作的最佳方法是什么?

标签: node.jsreactjsfilepond

解决方案


推荐阅读