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

<form enctype=multipart/form-data> is needed for files, but use:enhance masks it #9819

Closed
Rich-Harris opened this issue May 2, 2023 · 0 comments · Fixed by #9888
Closed
Assignees
Labels
breaking change bug Something isn't working low hanging fruit ready to implement please submit PRs for these issues!

Comments

@Rich-Harris
Copy link
Member

Describe the bug

If you have a form like this...

<form method="POST" use:enhance>
  <input name="file" type="file" required />
  <button>upload</button>
</form>

...then submitting the form will work correctly under normal conditions — the action will receive a FormData object with a file value of type File:

export const actions = {
  default: async ({ request }) => {
    const data = await request.formData();
    console.log(data.get('file')); // File {...}
  }
};

If you disable JavaScript, it works differently — data.get('file') will be a string (the file's name) instead of a File object. This is because the form needs enctype="multipart/form-data".

I'm not exactly sure what we should do here. If we explicitly set the content-type to form.enctype when we submit the form inside enhance, we still get different behaviour — file will be null for a JS submission but will still be a string for a no-JS submission. This platform really is an absolute clusterfuck.

I think the best thing to do is probably to print a warning in dev mode if the FormData object includes files and form.enctype !== 'multipart/form-data'. For SvelteKit 2 we could upgrade that to an error, but for now it would be a breaking change that is probably unwise.

Reproduction

later when I'm less busy

Logs

No response

System Info

latest

Severity

annoyance

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change bug Something isn't working low hanging fruit ready to implement please submit PRs for these issues!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants