
hi, i want to upload a image to a appwrite bucket, also i am using sveltekit. For some reason i get the error source.on is not a function, can someone tell me what i did wrong here because i feel like it is something dumb. how it works: user on the client uploads a image via form, form data gets sent to the backend, the image is processed on a backend and image should be sent to the bucket via backend
my code: frontend: +page.svelte
<script lang="ts">
import Input from '$lib/components/Input.svelte';
import Button from '$lib/components/Button.svelte';
import { enhance } from '$app/forms';
</script>
<div class="post-wrapper">
<h1>post joke</h1>
<form action="?/post" method="POST" enctype="multipart/form-data" use:enhance>
<Input name="title" type="text" placeholder="title" />
<Input type="file" accept="image/*" name="image" />
<Button>post joke</Button>
</form>
</div>
backend: +page.server.ts
import { ID, Databases, Client, Storage } from 'appwrite';
import { Buffer } from 'buffer';
import { InputFile } from 'node-appwrite/file';
export const actions = {
post: async ({ request }) => {
const data = await request.formData();
const image = data.get('image');
const arrayBuffer = await image.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const client = new Client()
.setEndpoint('https://cloud.appwrite.io/v1') // API Endpoint
.setProject('xxx'); // project ID
const storage = new Storage(client);
const result = await storage.createFile(
'xxx', // bucketId
ID.unique(),
InputFile.fromBuffer(buffer, 'sssss')
);
console.log(result);
}
};
i read the docs it says that i should use the InputFile when the image is processed on a backend, i made the bucket any with everything checked
thanks in advance.
Recommended threads
- How do I bulk add a large amount of imag...
Hello If anyone can help me adding a large amount of images to my storage that would be extremly helpful! I am trying to do it through the console or some sort ...
- Google consent screen is not showing aft...
Hey Appwrite team! 👋 I'm working on a Next.js app with Google OAuth integration using Appwrite, and I've run into a common UX scenario that could be improved. ...
- SSR FOR REACT VITE not next.js.
Good day all. Please I'd like to know if appwrite allows SSR for react app not vite. I checked the docs but I don't know I didn't see anything that specifies if...
