0

好的,我在这里无所适从。我在使用 Hooks 的功能组件中使用 FilePond 和 React。我没有使用 FilePond 来处理实际的上传,只是用文件设置状态,我的简化版本是这样的:

档案池:

        <FilePond          
            onupdatefiles={fileItems => handleFilepondUpdate(fileItems)}            
          />
        </Form.Field>

手柄更新:

 const handleFilepondUpdate = fileItems => {
    if (fileItems.length === 0) {
      addAttachment({
        ...picture,
        bugAttachment: null
      });
    } else {
      addAttachment({
        ...picture,
        bugAttachment: fileItems[0].file
      });   
    }
  };

状态:

const [picture, addAttachment] = useState({
    bugAttachment: ""
  });
const { bugAttachment } = picture;

最后我上传并清除输入状态:

 const onSubmit = e => {
    e.preventDefault();
    const fd = new FormData();
    fd.append("email", props.user[0].email);
    fd.append("bugDescription", bugDescription);
    fd.append("bugAttachment", bugAttachment);
    addBug(fd).then(() => {
      setBug({
        bugDescription: ""
      });

    });
  };

那么在发送表单后,我将如何删除 FilePond 文件呢?

4

1 回答 1

1

尝试使用 addAttachment 钩子清除bugAttachment里面的属性onSubmit

 const onSubmit = e => {
    e.preventDefault();
    const fd = new FormData();
    fd.append("email", props.user[0].email);
    fd.append("bugDescription", bugDescription);
    fd.append("bugAttachment", bugAttachment);
    addBug(fd).then(() => {
      setBug({
        bugDescription: ""
      });
      addAttachment({
        ...picture,
        bugAttachment:""
      });  

    });
  };

更新:您的图片状态似乎没有使用files道具,尝试这样的事情。

 <FilePond          
   files={bugAttachment}
   onupdatefiles={fileItems => handleFilepondUpdate(fileItems)}            
   />
于 2019-11-13T16:47:57.397 回答