Skip to content

Commit 850f668

Browse files
committed
chore(storage): test-blob
1 parent 3594bfb commit 850f668

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pages/test-blob.vue

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script setup>
2+
const form = ref(null)
3+
4+
async function upload() {
5+
await $fetch('/api/storage/upload', {
6+
method: 'POST',
7+
body: new FormData(form.value)
8+
})
9+
// form.value.reset()
10+
}
11+
</script>
12+
13+
<template>
14+
<form ref="form" @submit.prevent="upload">
15+
<input type="file" name="file" class="inline-block text-sm text-slate-500 file:mr-4 file:py-2 file:px-4 file:rounded-sm file:border-0 file:text-sm file:font-semibold file:bg-gray-200 file:text-gray-700 dark:file:bg-gray-700 dark:file:text-gray-200 hover:file:bg-gray-300 dark:hover:file:bg-gray-600">
16+
<UButton type="submit">
17+
Upload
18+
</UButton>
19+
</form>
20+
</template>

server/api/storage/upload.post.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default eventHandler(async (event) => {
2+
const form = await readFormData(event)
3+
const file = form.get('file') as Blob
4+
5+
if (!file || !file.size) {
6+
throw createError({ status: 400, message: 'No file provided' })
7+
}
8+
9+
if (file.type !== 'image/png' && file.type !== 'image/jpeg') {
10+
throw createError({ status: 400, message: 'File must be an image' })
11+
}
12+
13+
return useBlob().put(`images/${file.name}`, file)
14+
})

0 commit comments

Comments
 (0)