我正在尝试同时将多个图像上传到 firebase 存储,然后将它们调用到 firebase 数据库中。我有这个使用单一图像,但我不确定我需要如何修改我的代码以允许多个图像。当我选择要上传的多张图片时,它们成功上传到 firebase 存储,但是,只有上传的第一张图片会分配给 featuresImage 数组并因此显示。任何帮助将不胜感激。
这是我的代码:
class OtherEdit extends Component {
constructor(props) {
super(props);
this.state={
id: '',info: {
title: '',subtitle: ''
},article: {
featuredImage: []
}
}
}
...
uploadImageCallBack = (e) => {
return new Promise(async(resolve,reject) => {
const file = e.target.files[0]
const fileName = uuidv4()
storageRef.ref().child("OtherMedia/" + fileName).put(file)
.then( async snapshot => {
const downloadURL = await storageRef.ref().child("OtherMedia/" +fileName).getDownloadURL()
resolve({
success: true,data: {link: downloadURL},})
})
})
}
...
<FormGroup classname='edit-featured-image'> Featured images
<Input type="file" multiple accept='image/*' classname="image-uploader"
onChange={async (e) => {
const uploadState = await this.uploadImageCallBack(e)
if(uploadState.success) {
await this.setState({
hasFeaturedImage: true,article: {
...this.state.article,featuredImage: uploadState.data.link
}
})
}
}}>
</Input>
<div classname='image'>
{
this.state.hasFeaturedImage ?
<img src={this.state.article.featuredImage} /> : ''
}</div>
</FormGroup>