Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to upload multiple files with fields at once with formdata #559

Open
FlorentGrea opened this issue Feb 1, 2024 · 0 comments
Open

how to upload multiple files with fields at once with formdata #559

FlorentGrea opened this issue Feb 1, 2024 · 0 comments

Comments

@FlorentGrea
Copy link

FlorentGrea commented Feb 1, 2024

0

I have a pocketbase collection with "name", "image" and "order" fields. I'm trying to upload all the images with unique name and images using formdata at once.

here's some code:

`export default function AddPhotosAdmin({ photosDb }: PhotoDisplayProps) {
async function addPhotos(formData: FormData) {
'use server'

    const name = formData.get("name") as string
    let order = 0
    const dataPost = new FormData
    const Image_array = Array.from(formData.getAll("Image"))
    Image_array.map((image) => {
        dataPost.append('name', name)
        dataPost.append('Image', image)
        dataPost.append('order', (order++).toString())
    })
    const pb = new PocketBase(process.env.DB_ADDR);
    pb.collection('Photos').create(dataPost);
    revalidatePath("/Photos")
}
return (
    <div>
        <form action={addPhotos} className="text-black">
            <input name="name" />
            <input
                type="file"
                name="Image"
                required
                multiple
                accept="image/*"
                className="text-white"
            ></input>
            <input type="submit" className="text-white" value="Créer" />
        </form>
    </div>
)`

i tried a lot of ways to manage to send all the data at once, and using a loop POST is auto cancelled and seems not the good way to do it.

any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant