sitti ara
sitti ara

Reputation: 73

How to multiple image upload with React.js to Firebase

using react js I need multiple image upload to firebase I tried with below code is not working to multiple upload.

//display multi
handleChange(event) {

    const file = Array.from(event.target.files);
    this.setState({ file });   
}
//upload multi
fileuploadHandler = () => {

    const storageRef = fire.storage().ref();
    storageRef.child(`images/${this.state.file.name}`)
        .putFile(this.state.file).then((snapshot) => {

    });
  }

render() {
    return (
        <div className="App">
            <input id="file" type="file" onChange={this.handleChange.bind(this)} required multiple />
            <button onClick={this.fileuploadHandler}>Upload!</button>               
            </div>

    )

}

Upvotes: 2

Views: 5102

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599956

To handle multiple files, you'll need to loop over the files property of the input.

So:

const storageRef = fire.storage().ref();
this.state.file.forEach((file) => {
  storageRef
      .child(`images/${file.name}`)
      .putFile(file).then((snapshot) => {
  })
});

Upvotes: 3

Related Questions