如何使用EXPO将图像上传到Firebase?

时间:2020-10-09 18:57:38

标签: firebase react-native expo image-upload

这是我从github expo示例https://github.com/expo/examples/tree/master/with-firebase-storage-upload中提取的代码,用于将图像上传到Firebase存储。

 const pickImage = async () => {
        let pickerResult = await ImagePicker.launchImageLibraryAsync({
          allowsEditing: true,
          aspect: [4, 3],
        });
    
            handleImagePicked(pickerResult);
      };
      

      const handleImagePicked = async (pickerResult) => {
        try {
          
    
          if (!pickerResult.cancelled) {
             await uploadImageAsync(pickerResult.uri);
             console.log('done')
          }
        } catch (e) {
          console.log(e);
          alert('Upload failed, sorry :(');
        } finally {
        }
      };
    
      async function uploadImageAsync(uri) {
        // Why are we using XMLHttpRequest? See:
        // https://github.com/expo/expo/issues/2402#issuecomment-443726662
        const blob = await new Promise((resolve, reject) => {
          const xhr = new XMLHttpRequest();
          xhr.onload = function() {
            resolve(xhr.response);
          };
          xhr.onerror = function(e) {
            console.log(e);
            reject(new TypeError('Network request failed'));
          };
          xhr.responseType = 'blob';
          xhr.open('GET', uri, true);
          xhr.send(null);
        });
      
        const ref = firebase
          .storage()
          .ref()
          .child("images"+Math.random());
        const snapshot = await ref.put(blob);
      
        // We're done with the blob, close and release it
        blob.close();
      
        return await snapshot.ref.getDownloadURL();
      }

它在Android上运行良好,但在ios上给出错误消息,提示事件{isTrusted:false},网络请求失败。预先谢谢你

1 个答案:

答案 0 :(得分:0)

这是react-native的回归,已在最近的补丁程序发行版https://github.com/expo/expo/issues/10464#issuecomment-703178030

中修复

获得修复:

  • 从商店下载最新的iOS和Android客户端。
  • 使用expo client:install:ios和expo client:install:android安装最新的iOS和Android客户端