Skip to content

The behavior of FormData.append is quite strange #62111

@yarlc

Description

@yarlc

Version

24.14.0

Platform

Windows 10

Subsystem

No response

What steps will reproduce the bug?

Since fs.openAsBlob doesn't support large files, I decided to rewrite the File.stream function, but the result was unexpected.

class MyFile extends File {
    constructor() {
        super(["1"], "test.txt", { type: "text/plain" });
    }
    stream() {
        return new Response("2").body;
    }
}

async function f1() {
    const file = new MyFile();
    const form = new FormData();
    form.append("file", file, "othername.txt");
    const resp = new Response(form);
    console.log(await resp.text());
}

async function f2() {
    const file = new MyFile();
    const form = new FormData();
    form.append("file", file);
    const resp = new Response(form);
    console.log(await resp.text());
}

console.log(await f1()); // MyFile.stream has not been called, file content is "1"
console.log(await f2()); // MyFile.stream was called,          file content is "2"

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

The expected output of file content will be the same.

What do you see instead?

The content of the file has changed after being assigned a new name.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions