javascript - 使用 filepond 参数手动触发 filepond 中的服务器方法

标签 javascript reactjs filepond

我想在 filepond-react 组件中手动触发 fetch 方法。为了简化,我削减了其他 Prop 。

  // FileUpload Wrapper
  <div className="file-upload">
    <FilePond
      ref={this.props.innerref}
      files={this.state.files}
      onupdatefiles={this.updateFiles}
      {...this.props}
    />
  </div>

  // Calling the wrapper in the parent component
  <FileUpload
    innerref={this.fileRef}
    server={{
      fetch: this.onFileFetch,
      process: this.onUpload,
      revert: this.onFileRevert,
    }}
  />

服务器方法

// fetch
onFileFetch = async (url, load, error, progress, abort, headers) => {
  // function triggers but cannot access load, progress etc. here
}

// upload
onUpload = async (fieldName, file, metadata, load, error, progress, abort) => {
  // function triggers and can access load, error etc.
}

// delete/revert
onFileRevert = async (uniqueFileId, load, error) => {
  // function triggers and can access load, error etc.
}

行为和触发

因此,当我分别单击上传和恢复图标时,会触发上传和删除。我想手动触发 fetch() ,为此我调用 this.fileRef.current.props.server.fetch()

为什么无法访问该函数的参数? (负载、错误等)
我在下面添加了 filepond 对象(又名 this.fileRef.current)以供引用。

```
{current: FilePond}
  current: FilePond
    isMounted: (...)
    replaceState: (...)
    props:
      ref: (...)
      files: []
      instantUpload: false
      oninit: undefined
      labelIdle: "Drag file(s) here<br> or <br><br><span class="filepond--label-action">Browse</span>"
      onupdatefiles: ƒ (fileItems)
      innerref: {current: FilePond}
      className: "in-block s3-bucket-file-input"
      server:
        fetch: ƒ (url, load, error, progress, abort, headers)
        load: ƒ (source, load, error, progress, abort, headers)
        process: ƒ (fieldName, file, metadata, load, error, progress, abort)
        revert: ƒ (uniqueFileId, load, error)
        restore: ƒ (uniqueFileId, load, error, progress, abort, headers)
        remove: ƒ (source, load, error)
        __proto__: Object
      get ref: ƒ ()
      __proto__: Object
    context: {}
    refs: {}
    updater: {isMounted: ƒ, enqueueSetState: ƒ, enqueueReplaceState: ƒ, enqueueForceUpdate: ƒ}
    allowFilesSync: true
    _reactInternalFiber: FiberNode {tag: 1, key: null, stateNode: FilePond, elementType: ƒ, type: ƒ, …}
    _reactInternalInstance: {_processChildContext: ƒ}
    state: null
    _element: input.in-block.s3-bucket-file-input
    _pond: {on: ƒ, onOnce: ƒ, off: ƒ, setOptions: ƒ, addFile: ƒ, …}
    addFile: ƒ addFile(source)
    addFiles: ƒ addFiles()
    getFile: ƒ getFile(query)
    processFile: ƒ processFile(query)
    prepareFile: ƒ prepareFile(query)
    removeFile: ƒ removeFile(query)
    moveFile: ƒ moveFile(query, index)
    getFiles: ƒ getFiles()
    processFiles: ƒ processFiles()
    removeFiles: ƒ removeFiles()
    prepareFiles: ƒ prepareFiles()
    sort: ƒ sort(compare)
    browse: ƒ browse()
    __proto__: Component
    __proto__: Object
```

最佳答案

如果您手动调用 this.fileRef.current.props.server.fetch(),您不会将任何参数传递给该函数。 FilePond 在调用函数时会传递这些参数 url、load、error、progress、abort、headers,因此如果您想这样使用它,您也应该这样做。

这些函数不必标记为异步,它们可以是普通函数。

关于javascript - 使用 filepond 参数手动触发 filepond 中的服务器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59612648/

相关文章:

javascript - 如何在 Javascript 中更有效地执行检查和屏幕滚动?

javascript - 用 promise react native 嵌套对象循环

javascript - 模板文字作为 JSX 中的字符串内容

javascript - 如何使用 filepond 插件、ajax 和 php,在服务器上未检测到文件名

javascript - Filepond 自定义放置区

javascript - 检测 Bootstrap 模式窗口是否显示无法正常工作

javascript - 关闭 vue 中其他打开的开关

javascript - 插入媒体插件时如何防止tinymce删除URL源前缀?

reactjs - 模块解析失败 : Unexpected token (7:5) You may need an appropriate loader to handle this file type