Skip to content

FormData from <input type=file> with no file selected gives inconsistent results in browser vs Deno (empty file vs empty string) #24964

Open
@lionel-rowe

Description

Version: Deno 1.45.5

Given the following HTML:

<form method=post enctype="multipart/form-data">
    <input type=file name=files multiple>
    <input type=submit>
</form>

Assuming no files are selected, calling FormData#get('files') on a FormData object constructed from this form on the client side (tested latest Chrome, FireFox) gives an empty File object. However, calling (await req.formData()).get('files') on the Request given by Deno.serve gives an empty string "" instead.

Notably, FormData#getAll('files') gives an array of length 1 in both Deno and browsers, which hopefully everyone can agree is a horrible way of representing zero files, but the content of that array's element differs (again, it's an empty file on client side vs an empty string on Deno).

Repro: https://dash.deno.com/projects/formdata-no-files-repro

Activity

lionel-rowe

lionel-rowe commented on Aug 11, 2024

@lionel-rowe
ContributorAuthor

Observed browser behavior looks to be correct per the spec:

  1. Otherwise, if the field element is an input element whose type attribute is in the File Upload state, then:
    1. If there are no selected files, then create an entry with name and a new File object with an empty name, application/octet-stream as type, and an empty body, and append it to entry list.

Meanwhile, the raw HTTP request body looks something like this:

------WebKitFormBoundary5jMC0I81oPK5c8nU
Content-Disposition: form-data; name="files"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundary5jMC0I81oPK5c8nU--
added
bugSomething isn't working correctly
webrelated to Web APIs
on Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working correctlywebrelated to Web APIs

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      `FormData` from `<input type=file>` with no file selected gives inconsistent results in browser vs Deno (empty file vs empty string) · Issue #24964 · denoland/deno