Skip to content

Instantly share code, notes, and snippets.

@samocodes
Created June 16, 2023 17:00
Show Gist options
  • Save samocodes/d7c63d9ccce4c251345fcb0afbba0829 to your computer and use it in GitHub Desktop.
Save samocodes/d7c63d9ccce4c251345fcb0afbba0829 to your computer and use it in GitHub Desktop.
A simple code to upload blob on firebase storage.
<script lang="ts">
import firebaseApp from '$lib/firebase';
import { uploadBytes, getStorage, ref } from 'firebase/storage';
const onChange = (event: Event) => {
const target = event.target as HTMLInputElement;
if (!target) return;
if (target.files && target.files.length > 0) {
const selectedFile = target.files[0];
const fileBlob = new Blob([selectedFile], { type: selectedFile.type });
const storage = getStorage(firebaseApp);
const storageRef = ref(storage, `file/${selectedFile.name}`);
uploadBytes(storageRef, fileBlob)
.then((data) => console.log(data))
.catch((err) => console.error(err));
}
};
</script>
<input type="file" name="file" id="file" on:change={onChange} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment