【问题标题】:React native Failed to upload images to firebase反应原生无法将图像上传到firebase
【发布时间】:2021-01-05 19:10:31
【问题描述】:

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

{“code_”:“storage/invalid-argument”,“message_”:“Firebase 存储:put 索引 0 处的无效参数:预期的 Blob 或文件。”,“name_”:“FirebaseError”,“ serverResponse_": null}

我该如何解决这个问题?

我知道我必须将图像转换为文件或 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);
              });
          })

【问题讨论】:

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


    【解决方案1】:

    错误信息:

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

    告诉您您向put() 传递了一个无效值。如果您希望将 URL 传递给其他要上传的文件,那是行不通的。您只能上传运行代码的机器上本地存在的内容。

    如果您想传输另一个 URL 的内容,您将无法使用 Web 客户端 SDK 执行此操作。您必须编写一些后端代码来从 URL 下载内容,然后将其上传到 Cloud Storage。

    【讨论】:

    • 我正在尝试上传仅存在于我的模拟器中的图像,不起作用??
    • 您必须找到一种方法以 API 接受的方式引用该图像。
    猜你喜欢
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2021-05-22
    • 2018-10-13
    • 2021-09-18
    相关资源
    最近更新 更多