edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

`FormData` returns empty string instead of a `File`

Open f-elix opened this issue 11 months ago • 0 comments

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

f-elix avatar Jan 21 '25 15:01 f-elix