繁体   English   中英

如何使用expo上传图片到firebase?

[英]How to upload images to firebase using expo?

这是我从 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 上它给出了一个错误,说 Event {isTrusted: false},.network request failed。 先感谢您

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

获得修复:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM