Simple component to upload files from your svelte apps to uploadfly.
Install with your preferred package manager.
pnpm add uploadfly-svelte
yarn add uploadfly-svelte
To use this component make sure you've signed up to uploadfly and have your api key as an environment variable in env.local.
Don't forget to prefix with Vite in development.
VITE_UPLOADFLY_API_KEY=****
The fly component exports an uploaded event which tells us whether our file uploaded successfully
<script lang="ts">
import Fly from 'uploadfly-svelte'
function handle(e: Event) {
e.detail.success ?
// Handle successful upload
alert('Upload successful')
// handle error (show an error toast or display an alert)
: alert('Upload failed')
}
</script>
<Fly multiple on:uploaded={handle}/>
<script lang="ts">
import { Fly } from '$lib/index.js';
let fileUrl: string = ""
</script>
<Fly on:uploaded={(e) => fileUrl = e.detail.url} />
Events are emitted for each individual file upload, so when uploading multiple files, use a reactive function to capture the urls and store them in your database (or whatever is required)
<script lang="ts">
import { Fly } from '$lib/index.js';
let fileUrls: string[] = []
$: updateUrls = (url) => fileUrls = [...fileUrls, url]
</script>
<Fly
multiple
on:uploaded={(e) => updateUrls(e.detail.url)}
/>
Coming soon.
Coming soon.