Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在react native fetch blob中将多个图像上载到firebase_Javascript_React Native_Firebase Storage_React Native Android - Fatal编程技术网

Javascript 如何在react native fetch blob中将多个图像上载到firebase

Javascript 如何在react native fetch blob中将多个图像上载到firebase,javascript,react-native,firebase-storage,react-native-android,Javascript,React Native,Firebase Storage,React Native Android,我有多个图像存储在一个数组中(图像路径存储在数组中)。 然后我使用for循环上载每个图像,但只上载最后一个图像。 我使用react native fetch blob和firebase for(var i=0;ifirebase.storage() .ref(“用户照片”).child(“图像”+i) .put(blob,{contentType:'image/png'})。然后(()=>{ var storage=firebase.storage().ref(“userPhoto/”).ch

我有多个图像存储在一个数组中(图像路径存储在数组中)。 然后我使用for循环上载每个图像,但只上载最后一个图像。 我使用react native fetch blob和firebase

for(var i=0;ifirebase.storage()
.ref(“用户照片”).child(“图像”+i)
.put(blob,{contentType:'image/png'})。然后(()=>{
var storage=firebase.storage().ref(“userPhoto/”).child(“image”+i);
storage.getDownloadURL()。然后((url)=>{
var url=url;
});
})
);

}
好的,首先,您需要缓存数组的长度
this.state.imagesUri

这将使您的for循环看起来像for(var i=0,length=This.state.imagesUri.length;i,我希望您已经注意到您不再检查
i
(这是不正确的,因为
imagesUri
是一个数组)。

我希望这会有所帮助

onSend(图像){
让photo=images.map(img=>img.image);
photo.forEach((图像,i)=>{
const sessionId=new Date().getTime();
const Blob=RNFetchBlob.polyfill.Blob;
const fs=RNFetchBlob.fs;
window.XMLHttpRequest=
RNFetchBlob.polyfill.XMLHttpRequest;
Blob=Blob;
让uploadBlob=null;
让mime='image/jpg';
const imageRef=this.image.child(`${sessionId}${i}`);
fs.readFile(图像“base64”)
。然后((数据)=>{
返回Blob.build(数据,{type:`${mime};BASE64`})
})
.然后((blob)=>{
uploadBlob=blob;
返回imageRef.put(blob,{contentType:mime})
})
.然后(()=>{
uploadBlob.close();
返回imageRef.getDownloadURL()
})
。然后((url)=>{
console.log(url)
})
.catch((错误)=>{
});
})

}
我有此代码。要使用firebase和react native上的“rn fetch blob”上载多图像

export const uploadListImageMeal = (param) => {
  const { imagesUrls, mime = 'application/octet-stream', userID, time } = param

  const urls = imagesUrls.map((uri) => {
    const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
    // let uploadBlob = null
    const currentTime = Date.now()
    const imageRef = firebase.storage().ref(`images/${userID}/meal/${time}`).child(`${currentTime}.png`)
    return fs.readFile(uploadUri, 'base64')
      .then((data) => {
        return Blob.build(data, { type: `${mime};BASE64` })
      })
      .then((blob) => {
        // uploadBlob = blob
        return imageRef.put(blob._ref, blob, { contentType: mime })
      })
      .then(() => {
        // uploadBlob.close()
        return imageRef.getDownloadURL()
      })
      .then((url) => {
        return (url)
      })
      .catch((error) => {
        return host
      })
  })


  return Promise.all(urls)
    .then((data) => {
      return data
    })
}

对于(var i=0,length=this.state.imagesUri.length;ifor(var i=0、k=0、j=0等;Oh和@INyomanWiratmaJaya,如果你是新来的,那么我应该让你知道,如果这是你想要的答案,你可以点击上/下投票下面的大勾号。这有助于那些带着同样问题来到这里的人知道是什么解决了你的问题,也有助于我获得一些声誉n、 这可能是一个非常简单的修复…
for(让i=0
而不是
for(var i=0
您有那个代码,并且…?请描述一下,问题出在哪里,这段代码将如何解决它,以帮助未来的读者理解这个答案谢谢,forEach在forloop中确实工作得很好:)