#[SOLVED] Unable to upload file in react native mob app expo go

18 messages · Page 1 of 1 (latest)

left galleon
#

This is my function
export async function uploadFile(file, type) {
try {
if (!file || !file.assets || !file.assets[0]) {
throw new Error('Invalid file object provided');
}

        const {mimeType, ...rest} = file; // Extract necessary properties.
        const asset = {mimeType, ...rest}

        // Logging for debugging purposes
        console.log("Asset:", asset);
        console.log("Type:", type);

        // Assuming appwriteConfig is imported from somewhere
        const uploadedFile = await storage.createFile(
            appwriteConfig.storageId,
            ID.unique(),
            asset
        );

        // Logging for debugging purposes
        console.log("Uploaded File:", uploadedFile);

        // Assuming getFilePreview is defined elsewhere
        const fileUrl = await getFilePreview(uploadedFile.$id, type);

        return fileUrl;
    } catch (error) {
        // Rethrowing error with additional context
        throw new Error(`File upload failed: ${error.message}`);
    }
}
  
  // Get File Preview
  export async function getFilePreview(fileId, type) {
    let fileUrl;
  
    try {
      if (type === "video") {
        fileUrl = storage.getFileView(appwriteConfig.storageId, fileId);
      } else if (type === "image") {
        fileUrl = storage.getFilePreview(
          appwriteConfig.storageId,
          fileId, 2000, 2000, "top", 100
        );
      } else {
        throw new Error("Invalid file type");
      }
  
      if (!fileUrl) throw Error;
  
      return fileUrl;
    } catch (error) {
      throw new Error(error);
    }
  }
#

this is output
LOG Asset: {"assets": [{"assetId": null, "base64": null, "duration": null, "exif": null, "fileName": "1000000053.jpg", "filesize": 501109, "height": 2400, "mimeType": "image/jpeg", "rotation": null, "type": "image", "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Funtitled-f9b93264-058b-4952-b665-7546a2f16126/ImagePicker/307bead8-7750-41cf-b405-ec06454d4b4b.jpeg", "width": 1080}], "canceled": false, "mimeType": undefined}
LOG Type: image
LOG Uploaded File: undefined

arctic surge
left galleon
#

50

arctic surge
left galleon
#

idk it was some appwrite react native sdk

#

i installed it using this

#

npx expo install react-native-appwrite react-native-url-polyfill

#

sorry im very new to this

arctic surge
left galleon
#

oh can you please explain

arctic surge
#

If you look at the signature of the function, you'll see file should be {name: string, type: string, size: number, uri: string}

#

you're passing:

{"assets": [{"assetId": null, "base64": null, "duration": null, "exif": null, "fileName": "1000000053.jpg", "filesize": 501109, "height": 2400, "mimeType": "image/jpeg", "rotation": null, "type": "image", "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540anonymous%252Funtitled-f9b93264-058b-4952-b665-7546a2f16126/ImagePicker/307bead8-7750-41cf-b405-ec06454d4b4b.jpeg", "width": 1080}], "canceled": false, "mimeType": undefined}
left galleon
#

so i should just change the structure of asset?

#

let me give it a try

left galleon
#

Thank You so Much

#

You helped me alot