【问题标题】:how to use Dropzone in a react component如何在反应组件中使用 Dropzone
【发布时间】:2021-06-03 19:16:32
【问题描述】:

我正在尝试在反应组件中使用 Dropzone。 但它不起作用。变量上传文件始终未定义。

当我选择一个文件时,会发生这个错误。 错误。 TypeError:将循环结构转换为 JSON --> 从带有构造函数“HT​​MLInputElement”的对象开始 |属性“__reactFiber$35tlapmme54”-> 带有构造函数“FiberNode”的对象 --- 属性 'stateNode' 闭合圆圈

import React, { Component, useCallback } from 'react'
import {connect} from 'react-redux'
import { createProject } from '../../store/actions/projectActions'
import {Redirect} from 'react-router-dom'
import Dropzone  from 'react-dropzone';

class CreateProject extends Component {
    state = {
        title:'',
        content:'',
        uploadfile:'',
    }

    handleChange = (e) =>{
        this.setState({
            [e.target.id]: e.target.value
        })
    }

    handleSubmit = (e) =>{
        e.preventDefault();
        this.props.createProject(this.state)
        this.props.history.push('/')
    }

    onDrop = acceptedFiles => {
      if (acceptedFiles.length > 0) {
        this.setState({ uploadfile: acceptedFiles[0] })
      }
    }
  
    handleSubmitImg = (e) =>{
      e.preventDefault()
      //this.props.sampleteFunction()
    };

    

  render() {
   const maxSize = 3 * 1024 * 1024;
 

    const {auth} = this.props

    
    console.log("uploadFile"+this.uploadfile  ); //It always be Undefined
    if(!auth.uid) return <Redirect to="/signin" />
    return (
      <Dropzone
      onDrop={this.onDrop}
      accept="image/png,image/jpeg,image/gif,image/jpg"
      minSize={1}
      maxSize={maxSize}
    >
      {({ getRootProps, getInputProps }) => (

      <div className="container">
        <form onSubmit={this.handleSubmit} className="white">
            <h5 className="grey-text text-darken-3">
                Create Project
            </h5>
            <div className="input-field">
                <label htmlFor="title">Title</label>
                <input type="text" id="title" onChange={this.handleChange}/>
            </div>

            <div className="input-field">
                <label htmlFor="content">Project Content</label>
                <textarea id="content" className="materialize-textarea" onChange={this.handleChange}></textarea>
            </div>
            <div className="input-field">
                <button className="btn pink lighten-1 z-depth-0">Create</button>
            </div>
        </form>
            <div {...getRootProps()}>
                <input {...getInputProps()} />
                {console.log("SelectedFile"+ {...getRootProps() })}
                <p>Choose image File</p>
                {this.uploadfile ? <p>Selected file: {this.uploadfile.name}</p> : null}
            </div>

 
      </div>
  )}
</Dropzone>

    
    )
  }
}

const matchStateToProps = (state) => {
    return{
        auth: state.firebase.auth
    }
}

const mapDispatchToProps = (dispatch) => {
    return{
        createProject: (project) => dispatch(createProject(project))
    }
}

export default connect(matchStateToProps, mapDispatchToProps)(CreateProject)

【问题讨论】:

    标签: reactjs dropzone.js react-dropzone


    【解决方案1】:

    下面一行有错误

    {this.uploadfile ? <p>Selected file: {this.uploadfile.name}</p> : null}
    

    上传文件是一个状态变量,只能像这样访问

    this.state.uploadfile
    

    【讨论】:

      【解决方案2】:

      尝试解析接收到的文件。

      function parseFile(file) {
        const updatedFile = new Blob([file], { type: file.type });
        updatedFile.name = file.name;
      
        return updatedFile;
      }
      

      和 onDrop:

      onDrop={(files) => this.onDrop((
                      files.map((file) => parseFile(file))
                  ))}
      

      【讨论】:

      • ``` onDrop = acceptedFiles => { if (acceptedFiles.length > 0) { this.setState({ uploadfile: acceptedFiles[0] }) } } parseFile = (file) =>{ const updatedFile = new Blob([file], { type: file.type });更新文件名 = 文件名;返回更新文件; } render(){ this.onDrop(( files.map((file) => this.parseFile(file)) ))} accept="image/png,image/jpeg,图片/gif,图片/jpg" minSize={1} maxSize={maxSize} > ```
      • 感谢您的回答。我在上面进行了修改,但没有任何改变。
      • 抱歉发生了错误..Unhandled Rejection (TypeError): this.parseFile is not a function
      • 您需要从 this.parseFile 中删除它并将 parseFile 函数保留在您的反应类之外。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 2019-12-31
      • 2015-09-22
      相关资源
      最近更新 更多