1.0.2 • Published 1 year ago

@astro-metro/formidable v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Astro Formidable

Allow you to use formidable for request parse

Usage

pages/upload.json.ts

import {parseAstroForm, isFormidableFile} from '@astro-metro/formidable';

export const post: APIRoute = ({ request }) => {
    const formData: FormData = await parseAstroForm(Astro.request);
    let name = 'Not-File'

    const file = formData.get('file');
    if(isFormidableFile(file)){
        name = file.name;
    }

    return {
        body: name
    }
}

pages/index.page

---
import {parseAstroForm, isFormidableFile} from '@astro-metro/formidable';

if(Astro.request.method === "POST"){
    const formData: FormData = await parseAstroForm(Astro.request);

    const file = formData.get('my-file');
    if(isFormidableFile(file)){
        console.log('The user upload a file');
    }
}
---