2

使用 react js 我需要将多个图像上传到 firebase 我尝试使用以下代码无法进行多次上传。

//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>

    )

}
4

1 回答 1

3

要处理多个文件,您需要遍历files输入的属性。

所以:

const storageRef = fire.storage().ref();
this.state.file.forEach((file) => {
  storageRef
      .child(`images/${file.name}`)
      .putFile(file).then((snapshot) => {
  })
});
于 2018-06-10T16:24:48.810 回答