upload a file using Cloudflare Pages and SvelteKit

Hello,
I'm attempting to upload a file using Cloudflare Pages and SvelteKit. Below is the code snippet:
export const POST = async ({ request, locals }) => {
    const formData = Object.fromEntries(await request.formData())
    const image = formData.image

    console.log(image)
    /* 
    output on local environment: 
        File {
          size: 15307,
           type: 'image/png',
           name: 'image.png',
           lastModified: 1714500023653
        }
    output on prod environment (Cloudflare pages):
        " PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0001 \u0000\u0000\u0001 \b\u.....
    */

    if (!(image instanceof File)) {
        return json({ message: 'Invalid file' }, { status: 400 })
    }

    try {
        const objectKey = `key-1234`
        const uploadedImage = await locals.bucket.put(objectKey, image)

        let key = uploadedImage.key
        return json({ success: true, key: key }, { status: 200 })
    } catch (e) {
        return json({ message: e instanceof Error ? e.message : 'Failed to upload file ', err }, { status: 400 })
    }
}

This code works locally, but on the production environment (Cloudflare Pages), the image is not an instance of File.
Was this page helpful?