1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00

feat: Add File support in FormData (#4632)

This commit is contained in:
crowlKats 2020-04-05 21:49:04 +02:00 committed by GitHub
parent 2911fcc78d
commit 6720a0dc02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View file

@ -73,6 +73,15 @@ unitTest(function formDataParamsSetSuccess(): void {
assertEquals(formData.get("e"), "null"); 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 { unitTest(function formDataSetEmptyBlobSuccess(): void {
const formData = new FormData(); const formData = new FormData();
formData.set("a", new Blob([]), "blank.txt"); formData.set("a", new Blob([]), "blank.txt");

View file

@ -11,11 +11,18 @@ class FormDataBase {
[dataSymbol]: Array<[string, domTypes.FormDataEntryValue]> = []; [dataSymbol]: Array<[string, domTypes.FormDataEntryValue]> = [];
append(name: string, value: string): void; 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: 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); requiredArguments("FormData.append", arguments.length, 2);
name = String(name); 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, { const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type, type: value.type,
}); });
@ -70,8 +77,13 @@ class FormDataBase {
} }
set(name: string, value: string): void; 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: 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); requiredArguments("FormData.set", arguments.length, 2);
name = String(name); name = String(name);
@ -82,7 +94,9 @@ class FormDataBase {
while (i < this[dataSymbol].length) { while (i < this[dataSymbol].length) {
if (this[dataSymbol][i][0] === name) { if (this[dataSymbol][i][0] === name) {
if (!found) { 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, { const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type, type: value.type,
}); });
@ -101,7 +115,9 @@ class FormDataBase {
// Otherwise, append entry to the context objects entry list. // Otherwise, append entry to the context objects entry list.
if (!found) { 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, { const dfile = new domFile.DomFileImpl([value], filename || name, {
type: value.type, type: value.type,
}); });