Skip to content

FormData returns empty string instead of a File #1083

@f-elix

Description

@f-elix

Bug Report

When submitting a form with enctype="multipart/form-data" and an empty file input, the result of getting the value inside a Vercel edge function is an empty string.

const formData = await request.formData();
const file = formData.get('file');
console.log(file); // ''

If we do the same thing in a serverless function, we get a File object with a size of 0 instead, which is the expected behavior.

This messed up server-side form validation in one of my production site and was very hard to debug.

Additional context/screenshots

The test the issue, I ran the following code (using a Sveltekit server action) in both an edge function and a serverless function:

export const actions = {
	default: async (event) => {
		const formData = await event.request.formData();
		console.dir(formData, { depth: null });
		const name = formData.getAll('name');
		const file = formData.get('file');
		const files = formData.getAll('files');
		console.log({ name, file, files });
		console.log(file instanceof File);
		return {};
	}
};

Here are the logs from the Vercel dashboard.

Edge function log:

Image

Serverless function log:

Image
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions