Back

expo-image-picker and storage

  • 0
  • React Native
  • Storage
mrch33ks
10 Jan, 2025, 23:36

Hello, I'm looking for a working example, I've been struggling with this for days.

TypeScript
    console.log(file)
    const promise = storage.createFile(
      APPWRITE_AVATAR_STORAGE,
      ID.unique(),
      file
    );
    
    promise.then(function (response) {
      console.log("File uploaded successfully:", response); // Success
    }, function (error) {
      console.error("File upload failed:", error); // Failure
    });
  };


const selectImage = async () => {
    const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
    if (status !== 'granted') {
      alert('Camera roll permissions needed!');
      return;
    }

    const result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: 'images',
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });

    if (!result.canceled) {
      const selectedImage = result.assets[0];
      const fileUri = selectedImage.uri;

      setAvatar(fileUri);
      uploadAvatar(selectedImage);
    }
  }

It logs: "File uploaded successfully: undefined"

TL;DR
Developers are struggling with passing the correct format for the `createFile()` function in the `expo-image-picker` and storage scenario. One developer shared their code snippet for uploading an avatar but encountered an issue where it logged "File uploaded successfully: undefined."
mrch33ks
10 Jan, 2025, 23:38

I'm suspecting its the inputFile format I'm passing, I just can't seem to get it right.

it logs as:

TypeScript
{"assetId": "C52DD5F3-6856-4409-BC50-FC1178199FBB/L0/001", "base64": null, "duration": null, "exif": null, "fileName": "IMG_2312.jpg", "fileSize": 9571187, "height": 3026, "mimeType": "image/jpeg", "pairedVideoAsset": null, "type": "image", "uri": "file:///var/mobile/Containers/Data/Application/A458952A-7F30-480D-A0B9-2583C5F7F187/Library/Caches/ExponentExperienceData/@mrcheeks/airmail-drag/ImagePicker/154797BA-A28F-492E-A478-093376B0BAB0.jpg", "width": 3024}
Steven
10 Jan, 2025, 23:46

Look at the createFile() function. What does it expect for the 3rd parameter?

mrch33ks
10 Jan, 2025, 23:49

A binary file but I look for helpers in the SDK and there's none for react native?

Steven
10 Jan, 2025, 23:50

no look at the function signature

mrch33ks
10 Jan, 2025, 23:58

Thanks for you response, it requires a string, is that the uri?

Steven
10 Jan, 2025, 23:59

can you share a screenshot of what you're looking at that says it's a string?

mrch33ks
10 Jan, 2025, 23:59
Steven
11 Jan, 2025, 00:00

no look at the function signature of the actual SDK's createFile() function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more