Skip to content

Can't upload files through react-native (using expo-image-picker) #241

@saint-entity

Description

@saint-entity

I am making a react native application and I need to upload a file (specifically an Image) onto pocketbase through the app. However, pocketbase will upload the all the other information omitting the image. I have tried :

  • Uploading the image asset returned by expo-image-picker directly into pocketbase through the js-sdk in a dictionary
  • Uploading the image uri returned by expo-image-picker directly into pocketbase through the js-sdk in a dictionary
  • Uploading the image asset returned by expo-image-picker into a FormData into pocketbase through the js-sdk
  • Uploading the image uri returned by expo-image-picker into a FormData into pocketbase through the js-sdk
  • Using the image uri to fetch a blob from the file system to place in a dictionary to then upload to pocketbase
  • Using the image uri to fetch a blob from the file system to place in a FormData to then upload to pocketbase
  • Using the image uri to fetch a blob from the file system to place in a Javascript file object to place into a Dictionary/FormData to then upload to pocketbase
  • Using the image base64 to create a blob to place in a dictionary to then upload to pocketbase
  • Using the image base64 to create a blob to place in a Formdata to then upload to pocketbase
  • Using the image base64 to create a blob to place in a javascript file object to put in a dictionary to then upload to pocketbase
    const base64String = base64Data.startsWith("data:") ? base64Data.split(',')[1] : base64Data;
    const binaryString = decode(base64String);
    const uint8Array = new Uint8Array(binaryString.length);
    for (let i = 0; i < binaryString.length; i++) {
        uint8Array[i] = binaryString.charCodeAt(i);
    }
    const blob = new Blob([uint8Array], { type: "image/jpeg" });
    return blob;
}


  async function ImageCreateData() {
    const pb = new PocketBase(ngrok);
    try {
      let formData = new FormData()

        

        const addy = await pb.collection('Addresses').getFirstListItem(`Address = '${address.Address}' && User = '["${userId}"]'`);
        
        const imagefile = base64toFile(image.base64)

        const imgdata = {
              "Image": new File([imagefile], 'image'),
              "Address": addy.id,
              "Caption": caption,
              "User": `["${userId}"]`
        }


        const imgrecord = await pb.collection('Images').create(imgdata);

        navigation.goBack()

    } catch (error) {
      console.log("Error creating tenant:", error);
      setError(true)
    }
  }```
This was my last attempt help will be appreciated greatly

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions