Skip to content

Commit 5453f55

Browse files
MichaelDeBoeycwohlmanjacob-ebey
authored
fix(form-data): support empty File (#76)
* Fix submitting form data when file input is empty Co-authored-by: Jacob Ebey <[email protected]> * chore: revert stylistic changes * chore: fix changeset --------- Co-authored-by: Joshua Ohlman <[email protected]> Co-authored-by: Jacob Ebey <[email protected]>
1 parent c4980f4 commit 5453f55

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

.changeset/lemon-nails-rhyme.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@web-std/form-data": patch
3+
---
4+
5+
Fix submitting form data when file input is empty. Addresses https://github.com/remix-run/remix/pull/3576
6+

packages/form-data/src/form-data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ const toEntryValue = (value, filename) => {
240240
} else if (isBlob(value)) {
241241
return new BlobFile([value], filename != null ? filename : "blob")
242242
} else {
243-
if (filename != null) {
243+
if (filename != null && filename != "") {
244244
throw new TypeError(
245245
"filename is only supported when value is Blob or File"
246246
)

packages/form-data/test/form-data.spec.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ export const test = test => {
8989
assert.equal(file2.lastModified, 123, "lastModified should be 123")
9090
})
9191

92+
// This mimics the payload sent by a browser when a file input
93+
// exists but is not filled out.
94+
test("filename on string contents", () => {
95+
const formData = new FormData()
96+
formData.set("file-3", new Blob([]), "")
97+
const file3 = /** @type {File} */ (formData.get("file-3"))
98+
assert.equal(file3.constructor.name, "File")
99+
assert.equal(file3.name, "")
100+
assert.equal(file3.type, "")
101+
})
102+
92103
test("throws on few args", () => {
93104
const data = new FormData()
94105
// @ts-expect-error
@@ -207,21 +218,21 @@ export const test = test => {
207218
assert.deepEqual([...data], [["n2", "v2"]])
208219
})
209220

210-
test("Shold return correct filename with File", () => {
221+
test("Should return correct filename with File", () => {
211222
const data = new FormData()
212223
data.set("key", new File([], "doc.txt"))
213224
const file = /** @type {File} */ (data.get("key"))
214225
assert.equal("doc.txt", file.name)
215226
})
216227

217-
test("Shold return correct filename with Blob filename", () => {
228+
test("Should return correct filename with Blob filename", () => {
218229
const data = new FormData()
219230
data.append("key", new Blob(), "doc.txt")
220231
const file = /** @type {File} */ (data.get("key"))
221232
assert.equal("doc.txt", file.name)
222233
})
223234

224-
test("Shold return correct filename with just Blob", () => {
235+
test("Should return correct filename with just Blob", () => {
225236
const data = new FormData()
226237
data.append("key", new Blob())
227238
const file = /** @type {File} */ (data.get("key"))

0 commit comments

Comments
 (0)