Skip to content
Init is coming / May 19 - 23
Back

File upload from buffer

  • 0
  • React Native
  • Cloud
SorryLad2
1 Dec, 2024, 20:29

Hey guys, I have read the documentation and must be missing something. I am trying to zip a few images and upload the zip to appwrite. What am I doing wrong here?

I am in react-native. It appears react-native-appwrite SDK doesn't have the InputFile class?

TypeScript
// Function to zip images and upload to Appwrite
const zipAndUploadImages = async (selectedImages) => {
  console.log("Attempting to zip and upload selected images...");
  try {
    const zip = new JSZip();

    for (let i = 0; i < selectedImages.length; i++) {
      console.log(`Fetching image: ${selectedImages[i]}`);
      const response = await fetch(selectedImages[i]);
      const arrayBuffer = await response.arrayBuffer();

      console.log(`Adding image_${i + 1}.jpg to zip`);
      zip.file(`image_${i + 1}.jpg`, arrayBuffer);
    }

    console.log('Creating zip file...');
    
    // Generate zip as Base64
    const zipBase64 = await zip.generateAsync({ type: 'base64' });

    console.log('Uploading zip file to Appwrite collection...');
    const buffer = Uint8Array.from(atob(zipBase64), (char) => char.charCodeAt(0)); // Convert Base64 to Uint8Array

    try{
      const file = await storage.createFile(
        config.appwriteFilesId, // Replace with your Appwrite bucket ID
        ID.unique(),
        buffer
      );
      console.log('File uploaded successfully!');
      return file.$id;
    }
    catch (error){
      console.error('Error storing zip in appwrite.', error)
      return null;
    }
  
  } catch (error) {
    console.error('Error zipping or uploading images:', error);
    return null;
  }
};
TL;DR
Developers are trying to zip images and upload the zip to Appwrite in React Native but facing issues. The `InputFile` class is not available in react-native-appwrite SDK. The solution involves converting the zip file to Base64 and then to Uint8Array before uploading it using the `storage.createFile` function provided by Appwrite.
SorryLad2
1 Dec, 2024, 20:29

Focus on this part

TypeScript
const file = await storage.createFile(
        config.appwriteFilesId, // Replace with your Appwrite bucket ID
        ID.unique(),
        buffer
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