File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
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 >
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments