File Uploads
multipart/form-data parsed into typed file fields
The problem: a user uploads a profile picture. You need the file and the other form fields, type-checked, without hand-parsing the multipart body.
The fix: send a FormData to execute(). nuxt-actions parses each file part into a typed { filename, type, data } field, and your handler validates the rest with Zod.
uploadProgress: true reports how much of the file has been sent. A small image on a fast connection finishes at once; throttle the network in your browser's developer tools to watch it move.
Upload an avatar
How it works
// server/actions/upload-avatar.post.ts
export default defineAction({
input: z.object({ label: z.string(), avatar: z.any().optional() }),
handler: async ({ input }) => {
const file = input.avatar as { filename: string, type: string, data: Buffer }
return { filename: file.filename, size: file.data.length }
},
})
// client
const { execute, uploadProgress } = useAction(uploadAvatar, { uploadProgress: true })
const fd = new FormData()
fd.append('label', label)
fd.append('avatar', file)
await execute(fd)