SvelteKit
Upload a file from your Svelte Kit App
Install the Javascript SDK.
pnpm add @uploadfast/client
npm i @uploadfast/client
bun add @uploadfast/client
+page.ts
import { fail } from '@sveltejs/kit';
import { createClient } from '@uploadfast/client'
import { UPLOADFAST_API_KEY } from '$env/static/private';
export const actions = {
// You can use a named action for example.
upload: async ({ request }) => {
const formData = Object.fromEntries(await request.formData());
const fast = createClient
if (
!(formData.file as File).name ||
(formData.file as File).name === 'undefined'
) {
return fail(400, {
error: true,
message: 'You must provide a file to upload'
});
}
const { file } = formData as { file: File };
const fast = createClient({apiKey: UPLOADFAST_API_KEY})
const fileData = await fast.upload(file)
const { url, file_name } = fileData[0]
// Store meta data in your database
}
};
+page.svelte
Your client code could look like this.
<form method="post" use:enhance enctype="multipart/form-data" action="?/upload">
<div class="group">
<label for="file">Upload your profile picture</label>
<input type="file" id="file" name="file" />
</div>
<button type="submit">Submit</button>
</form>