Skip to content
Back

Can't upload image to the Appwrite storage.

  • 1
  • React Native
Economics
10 Apr, 2025, 18:07

// Preparing the file `const setImageToCloud = async () => { setLoading(true); try { // Determine MIME type based on file extension if (!selectedImage) { throw new Error("No image selected"); }

TypeScript
  const response = await fetch(selectedImage.uri); // fetch the local file
  const blob = await response.blob(); // get file data as Blob
  const fileObj = new File([blob], "photo.jpg", { type: blob.type });
  // Now upload to Appwrite:

  const res = await createFile({ file: fileObj! });

  if (res.success) {
    console.log("Image uploaded successfully");
    setHideDrawer(true);
    setImage(selectedImage?.uri!);
  } else {
    console.log("Image upload failed");
    Alert.alert("Image upload failed");
  }
} catch (error) {
  console.log("Error uploading image", error);
  Alert.alert("Error uploading image");
} finally {
  setLoading(false);
}

};`

// For uploading the file to Appwrite `export const createFile = async ({ file }: { file: any }) => { try { const result = await storage.createFile( appwriteConfig.storageBucketOneId!, ID.unique(), file )

TypeScript
    console.log("File created:", result);

    return { success: true }
} catch (error) {
    console.log("Error creating file:", error);
    return { success: false };
}

}`

TL;DR
Developers are unable to upload an image to Appwrite storage. The issue might be due to the function that prepares and uploads the file. The function createFile for uploading the file to Appwrite seems to be working correctly. Possible solution: Ensure the selectedImage object is properly defined before trying to upload the file.
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