EconomicsOriginal post
React Developer
// Preparing the file
`const setImageToCloud = async () => {
setLoading(true);
try {
// Determine MIME type based on file extension
if (!selectedImage) {
throw new Error("No image selected");
}
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
)
console.log("File created:", result);
return { success: true }
} catch (error) {
console.log("Error creating file:", error);
return { success: false };
}
}`
Summary
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.