How to upload files using React?

前端 未结 2 933
刺人心
刺人心 2020-12-19 09:18

so currently I am trying to upload a file, and save it on the Google Clouds. But I am stuck at how to get the input file using React-Redux.

Basically, my back-end pa

相关标签:
2条回答
  • 2020-12-19 09:35

    Checkout this blog, very well explained upload feature in react with example. Below is upload code from above blog:

    handleUploadedFiles = files => {
            var reader = new FileReader();
            reader.onload = (e) => {
                //Split csv file data by new line so that we can skip first row which is header
                let jsonData = reader.result.split('\n');    
                let data = [];
                jsonData.forEach((element, index) => {
                    if(index) {
                         //Split csv file data by comma so that we will have column data
                        const elementRaw = element.split(',');
                        console.log(element, index);
                        if(element) {
                            let param = {
                                'id' : elementRaw[0],
                                'name' : elementRaw[1],
                                'age' : elementRaw[2],
                                'address' : elementRaw[3]
                            }
                            data.push(param);
                        }
                    }
                });
            }
            console.log("TCL: Dashboard -> reader.readyState", reader.readyState)          
            reader.readAsText(files[0]);
        }
    
    0 讨论(0)
  • 2020-12-19 09:52

    You cant handle onChange file by method <input type="file" onChange={this.handleChangeFile.bind(this)}>

    handleChangeFile(event) {
      const file = event.target.files[0];
      let formData = new FormData();
      formData.append('file', file);
      //Make a request to server and send formData
    }
    
    0 讨论(0)
提交回复
热议问题