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

FormData returns empty string instead of a File #1083

Open
f-elix opened this issue Jan 21, 2025 · 0 comments
Open

FormData returns empty string instead of a File #1083

f-elix opened this issue Jan 21, 2025 · 0 comments

Comments

@f-elix
Copy link

f-elix commented Jan 21, 2025

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

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