Skip to content

Commit

Permalink
feat: Add File support in FormData (#4632)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Apr 5, 2020
1 parent 2911fcc commit 6720a0d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
9 changes: 9 additions & 0 deletions cli/js/tests/form_data_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ unitTest(function formDataParamsSetSuccess(): void {
assertEquals(formData.get("e"), "null");
});

unitTest(function fromDataUseDomFile(): void {
const formData = new FormData();
const file = new File(["foo"], "bar", {
type: "text/plain",
});
formData.append("file", file);
assertEquals(formData.get("file"), file);
});

unitTest(function formDataSetEmptyBlobSuccess(): void {
const formData = new FormData();
formData.set("a", new Blob([]), "blank.txt");
Expand Down
26 changes: 21 additions & 5 deletions cli/js/web/form_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ class FormDataBase {
[dataSymbol]: Array<[string, domTypes.FormDataEntryValue]> = [];

append(name: string, value: string): void;
append(name: string, value: domFile.DomFileImpl): void;
append(name: string, value: blob.DenoBlob, filename?: string): void;
append(name: string, value: string | blob.DenoBlob, filename?: string): void {
append(
name: string,
value: string | blob.DenoBlob | domFile.DomFileImpl,
filename?: string
): void {
requiredArguments("FormData.append", arguments.length, 2);
name = String(name);
if (value instanceof blob.DenoBlob) {
if (value instanceof domFile.DomFileImpl) {
this[dataSymbol].push([name, value]);
} else if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type,
});
Expand Down Expand Up @@ -70,8 +77,13 @@ class FormDataBase {
}

set(name: string, value: string): void;
set(name: string, value: domFile.DomFileImpl): void;
set(name: string, value: blob.DenoBlob, filename?: string): void;
set(name: string, value: string | blob.DenoBlob, filename?: string): void {
set(
name: string,
value: string | blob.DenoBlob | domFile.DomFileImpl,
filename?: string
): void {
requiredArguments("FormData.set", arguments.length, 2);
name = String(name);

Expand All @@ -82,7 +94,9 @@ class FormDataBase {
while (i < this[dataSymbol].length) {
if (this[dataSymbol][i][0] === name) {
if (!found) {
if (value instanceof blob.DenoBlob) {
if (value instanceof domFile.DomFileImpl) {
this[dataSymbol][i][1] = value;
} else if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type,
});
Expand All @@ -101,7 +115,9 @@ class FormDataBase {

// Otherwise, append entry to the context object’s entry list.
if (!found) {
if (value instanceof blob.DenoBlob) {
if (value instanceof domFile.DomFileImpl) {
this[dataSymbol].push([name, value]);
} else if (value instanceof blob.DenoBlob) {
const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type,
});
Expand Down

0 comments on commit 6720a0d

Please sign in to comment.