react-dropzone-如何通过文件夹读取文件?

时间:2018-10-03 15:53:45

标签: reactjs dropzone react-dropzone

我正在尝试使用react-dropzone,并且试图读取已拖到该区域中的文件夹的所有图像。

我看到他们有一个example

const { getDroppedOrSelectedFiles } = require('html5-file-selector')

class FolderDropzone extends React.Component {
  constructor() {
    super()
    this.state = { files: [] }
  }

  onDrop(files) {
    this.setState({
      files
    })
  }

  render() {
    return (
      <section>
        <div className="dropzone">
          <Dropzone
            getDataTransferItems={evt => getDroppedOrSelectedFiles(evt).then(files => files)}
            onDrop={this.onDrop.bind(this)}
          >
            <p>Drop a folder with files here.</p>
          </Dropzone>
        </div>
        <aside>
          <h2>Dropped files and folders</h2>
          <ul>
            {this.state.files.map(f => (
              <li key={f.name}>
                {f.fullPath} - {f.size} bytes
              </li>
            ))}
          </ul>
        </aside>
      </section>
    )
  }
}

但它似乎不起作用,也没有说明使用了哪个第三方插件。

我的本​​地副本出现此错误

index.js:249 Failed to generate preview for file {fileObject: File(180093), fullPath: "1.jpeg", lastModified: 1538421964806, lastModifiedDate: Mon Oct 01 2018 12:26:04 GMT-0700 (Pacific Daylight Time), name: "1.jpeg", …} TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
    at index.js:246
    at Array.forEach (<anonymous>)
    at index.js:243

        fileList.forEach(function (file) {
          if (!disablePreview) {
            try {
              file.preview = window.URL.createObjectURL(file); // eslint-disable-line no-param-reassign
            } catch (err) {
              if (process.env.NODE_ENV !== 'production') {
                console.error('Failed to generate preview for file', file, err); // eslint-disable-line no-console
              }
            }
          }

1 个答案:

答案 0 :(得分:0)

我有这个确切的要求,就像使用antd一样容易,就像使用rc-upload一样。像超级按钮一样工作,您还可以获得允许的文件类型的相对路径。