TheTechGuy
TheTechGuy

Reputation: 1608

ReactJS file upload

I am trying to upload a file using reactjs. I am not getting the right log. Before uploading, I wanted to see the output. But not getting the result.

Here what I have tried

state = {
    selectedFile: null
}

fileChangedHandler = event => {
    this.setState({
        selectedFile: event.target.files[0]
    })
    console.log(this.state.selectedFile) 
}
uploadHandler = () => {
    const formData = new FormData()
    var fd = formData.append("data", this.state.selectedFile, this.state.selectedFile.name)
    console.log(fd)

}

render() {
    return (
        <div>
            <input type="file" onChange={this.fileChangedHandler} />
            <button onClick={this.uploadHandler}>Upload!</button>
        </div>
    );
}

Upvotes: 0

Views: 155

Answers (1)

Parag Soni
Parag Soni

Reputation: 713

Try this

// Create your FormData object
    var formData = new FormData();
    formData.append('key1', 'value1'); // Test data
    formData.append('key2', 'value2'); // Test data
    
    // Display the key/value pairs array
    for (var pair of formData.entries()) {
        console.log(pair); 
    }

Upvotes: 1

Related Questions