react js handling file upload

后端 未结 7 957
长发绾君心
长发绾君心 2020-12-09 11:29

I\'m new to react js. I want to upload image asynchronously with react js Suppose I have this code

var FormBox = React.createClass({
  getInitialState: funct         


        
7条回答
  •  醉酒成梦
    2020-12-09 12:10

    import axios from 'axios';
    var FormBox = React.createClass({
      getInitialState: function () {
        return {
          photo: [],
          name : '',
          documents:[]
        }
      },
      pressButton: function () {
        var component = this
        var data = new FormData();
        data.append("photo", component.state.photo, component.state.name);
        var request = axios.post('http://localhost:3000/document', data)
            request.then(function(response){
        // you need to send data from server in response
              if(response.status == 200){
                 console.log('saved in db')
                 component.state.documents.push(response.data.documents)
                 // pushed document data in documents array
               }
            })
    
    
      },
      getPhoto: function () {
        var uploadfile = document.getElementById(upload_doc).files[0]
        this.setState({
          photo: uploadfile, name : uploadfile.name
        })
      },
      render: function () {
        var documents = this.state.documents.map((doc)=>{
           return 
        })
       // you can show your documents uploaded this way using map function
        return (
          
    {documents} // this way you can see uploaded documents
    ) } }) ReactDOM.render(, document.getElementById('root'))

提交回复
热议问题