简体   繁体   English

React Native 无法将图像上传到 Firebase

[英]React native Failed to upload images to firebase

I am trying to upload image to firebase storage or firestore and i am getting the following error:我正在尝试将图像上传到 firebase 存储或 firestore,但出现以下错误:

{"code_": "storage/invalid-argument", "message_": "Firebase Storage: Invalid argument in put at index 0: Expected Blob or File.", "name_": "FirebaseError", "serverResponse_": null} {"code_": "storage/invalid-argument", "message_": "Firebase Storage: Invalid argument in put at index 0: Expected Blob or File.", "name_": "FirebaseError", "serverResponse_": null}

how can I resolve this?我该如何解决这个问题?

I know I have to convert the image to file or blob format,我知道我必须将图像转换为文件或 blob 格式,

but how can I do this, in this case??但在这种情况下,我该怎么做?

PS: I am NOT using EXPO PS:我没有使用 EXPO

below is my function that takes the image下面是我拍摄图像的函数

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]);

below is how I Am trying to upload that image to firebase:以下是我尝试将该图像上传到 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);
              });
          })

The error message:错误信息:

Invalid argument in put at index 0: Expected Blob or File.放置在索引 0 处的参数无效:预期的 Blob 或文件。

Is telling you that you passed an invalid value to put() .告诉您您向put()传递了一个无效值。 If you're hoping to pass a URL to some other file to upload, that won't work.如果您希望将 URL 传递给其他要上传的文件,那将行不通。 You can only upload content that exists locally on the machine running the code.您只能上传运行代码的机器上本地存在的内容。

If you want to transfer the contents of another URL, you won't be able to do that with the web client SDK.如果要传输另一个 URL 的内容,则无法使用 Web 客户端 SDK 执行此操作。 You will have to write some backend code to download the content from the URL, then upload it to Cloud Storage.您必须编写一些后端代码才能从 URL 下载内容,然后将其上传到 Cloud Storage。

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

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