【问题标题】:How to upload images to firebase using expo?如何使用expo将图像上传到firebase?
【发布时间】:2021-01-24 21:02:41
【问题描述】:

这是我从 github expo examples 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 上它给出了一个错误,提示 Event {isTrusted: false},网络请求失败。提前谢谢你

【问题讨论】:

    标签: firebase react-native expo image-upload


    【解决方案1】:

    这是 react-native 的回归,它已在最近的补丁版本中得到修复:https://github.com/expo/expo/issues/10464#issuecomment-703178030

    获得修复:

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

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 2021-06-07
      • 2020-06-30
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 2022-12-03
      • 2021-12-16
      • 1970-01-01
      相关资源
      最近更新 更多