响应本机无法将图像上传到Firebase

时间:2020-09-18 20:45:41

标签: javascript firebase react-native google-cloud-firestore google-cloud-storage

我正在尝试将图像上传到Firebase存储或Firestore,但出现以下错误:

{“代码_”:“存储/无效参数”,“消息_”:“ Firebase存储:put中索引0处的无效参数:预期的Blob或文件。”,“ name _”:“ FirebaseError” ,“ serverResponse_”:空}

我该如何解决?

我知道我必须将图像转换为文件或blob格式,

但是在这种情况下,我该怎么做?

PS:我没有使用EXPO

下面是我拍摄照片的功能

const handleSelectPicture = useCallback(() => {
    ImagePicker.showImagePicker(
      {
        noData: true,
        title: 'Selecione a foto desejada',
        cancelButtonTitle: 'Cancelar',
        takePhotoButtonTitle: 'Usar câmera',
        maxWidth: 800,
        maxHeight: 800,
        chooseFromLibraryButtonTitle: 'Escolher da galeria',
      },
      response => {
        if (response.didCancel) {
          return;
        }

        if (response.error) {
          Alert.alert('Erro ao fazer upload da foto, tente novamente.');
          return;
        }

        console.log('image: >>>> ', response.uri);

        setImageURI({ uri: response.uri });

        console.log('State uri>>>>', imageURI);
      },
    );
  }, [imageURI]);

以下是我尝试将该图像上传到Firebase的方法:

    await firebase
          .auth()
          .createUserWithEmailAndPassword(
            email,
            password,
          )
          .then(async auth => {
            console.log('valueee', auth);

            await db
              .collection('users')
              .doc('userData')
              .set({

                photo: imageURI,
                document: params.fileSelected,
                address: [data],
              });

            firebase
              .storage()
              .ref(`users/${auth.user.uid}/profileImage.jpg`)
              .put(imageURI.uri)
              .then(() => {
                setUser(auth);
              });
          })

1 个答案:

答案 0 :(得分:0)

错误消息:

输入索引0处的参数无效:预期的Blob或文件。

告诉您您将无效值传递给put()。如果您希望将URL传递到其他文件以进行上传,则此操作将无效。您只能上传运行代码的计算机上本地存在的内容。

如果您要传输其他URL的内容,则无法使用Web客户端SDK进行。您将必须编写一些后端代码才能从URL下载内容,然后将其上传到Cloud Storage。