Skip to content
Back

Issue with storage.createFile() from mobile

  • 0
  • Android
  • Apple
  • Storage
  • React Native
  • Self Hosted
KejDzi
2 Aug, 2024, 13:15

Hi, I'm new to appwrite so it can be obvious mistake but I'm stuck and don't know how to move on :/

Here is my problem: I'm making an app in react native. I want to change profile image by deleting old one from storage and upload newest image. Everything is working fine on web browser, but when I'm trying to do it on my ios the created file has size 0 and mimeType application/x-empty. Before creating file I'm checking the size and type of chosen image and it's all good.

Here are my fragments of code to do so:

  • conventer uri to file "" const uriToFile = async (uri, fileName, imageType) => { const response = await fetch(uri); const blob = await response.blob(); console.log('Blob type: ', blob.type); const file = new File([blob], fileName, { type: imageType }); return file; };

""

"" const updateProfilePicture = async (userId, oldFileId, newFileUri, documentId, imageType) => { try { // Delete

TypeScript
  await storage.deleteFile('66a0fb33002ea2c56095', oldFileId);
  console.log("File deleted successfully.");

  // Upload
  const newFile = await uriToFile(newFileUri, userId, imageType);
  const newFileUploaded = await storage.createFile(
    '66a0fb33002ea2c56095',
    ID.unique(),
    newFile
  );

  console.log('Uploaded');
  console.log('New file: ', newFile);
  console.log('NewFile-size: ', newFile.size);
  console.log('New file type: ', newFile.type);
  console.log('After createFile(): ', newFileUploaded);
  
  // Update id
  await databases.updateDocument(
    '669a22000039eeadb976',
    '66a0fb5b001f6434e99a',
    documentId,
    { image_id: newFileUploaded.$id }
  );
  console.log('Image Id: ', newFileUploaded.$id);
  console.log("Updated");
  console.log('Profile picture updated successfully!');
} catch (error) {
  console.error('Error updating profile picture:', error);
}

}; "" I will appreciate any help 🙂

TL;DR
Developers are facing an issue with storage.createFile() from mobile in React Native. The created file on iOS has a size of 0 and a mimeType of application/x-empty. The solution involves checking the size and type of the chosen image before creating the file. To resolve the problem, developers should ensure that the file is correctly converted from URI to a file object. They can utilize the provided code snippets to delete the old file from storage and upload the new image successfully for profile updates.
KejDzi
2 Aug, 2024, 13:15

Here are also my console logs: "" LOG Uri of new image: file:///var/mobile/Containers/Data/Application/4E1A122C-7196-46B1-BC00-F0EC39DE2990/Library/Caches/ExponentExperienceData/@anonymous/cytrynka-e449390d-4def-455f-a51a-25d143c301ad/ImagePicker/7383A1B8-BF63-49DF-AC1E-C78A8622006D.png LOG Name of new image: IMG_4848.png LOG MimeType of new image: image/png LOG File deleted successfully. LOG File-size: 140563 LOG Uploaded LOG New file: {"_data": {"__collector": {}, "blobId": "88983c4d-6d8d-40c0-a6af-678939988ded", "lastModified": undefined, "name": "6699130f0025813268c7", "offset": 0, "size": 140563, "type": "image/png"}} LOG NewFile-size: 140563 LOG New file type: image/png LOG After createFile(): {"$createdAt": "2024-08-02T13:11:26.079+00:00", "$id": "66acdafd001ee32264dc", "$permissions": ["read(user:6699130f0025813268c7)", "update(user:6699130f0025813268c7)", "delete(user:6699130f0025813268c7)"], "$updatedAt": "2024-08-02T13:11:26.079+00:00", "bucketId": "66a0fb33002ea2c56095", "chunksTotal": 1, "chunksUploaded": 1, "mimeType": "application/x-empty", "name": "6699130f0025813268c7", "signature": "d41d8cd98f00b204e9800998ecf8427e", "sizeOriginal": 0} LOG Image Id: 66acdafd001ee32264dc LOG Updated LOG Profile picture updated successfully! ""

KejDzi
2 Aug, 2024, 13:17

And the way how I'm getting an image: "" const changeProfileImage = async () => { let permissionResult = await ImagePicker.requestMediaLibraryPermissionsAsync();

TypeScript
if (permissionResult.granted === false) {
  Alert.alert("Permission to access camera roll is required!");
  return;
}

let pickerResult = await ImagePicker.launchImageLibraryAsync({
  mediaTypes: ImagePicker.MediaTypeOptions.Images,
  allowsEditing: true,
  aspect: [4, 3],
  quality: 1,
});

if (!pickerResult.canceled) {
  const newImageUri = pickerResult.assets[0].uri;
  const newImageFileName = pickerResult.assets[0].fileName;
  const newImageType = pickerResult.assets[0].mimeType;
  console.log("Uri of new image: ", newImageUri);
  console.log("Name of new image: ", newImageFileName);
  console.log("MimeType of new image: ", newImageType);

  const user = await account.get("current");
  const userProfile = profileData.find(profile => profile.user_id === user.$id);
  // userProfile is for get correct Id of image for current user from my collection
  if (userProfile) {
    await updateProfilePicture(user.$id, userProfile.image_id, newImageUri, userProfile.$id, newImageType);
    setProfileImage(newImageUri);
  }
}

}; ""

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