Created
June 16, 2023 17:00
-
-
Save samocodes/d7c63d9ccce4c251345fcb0afbba0829 to your computer and use it in GitHub Desktop.
A simple code to upload blob on firebase storage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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