0

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>
    );
}
1

1 Answer 1

1

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); 
    }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.