@@ -19,6 +19,7 @@ The File API in Andromeda allows you to:
1919- Handle file metadata (name, size, type, modification time)
2020- Work with Blob objects for binary data
2121- Process files in a standardized way
22+ - Handle FormData for form submissions and multipart data
2223
2324## Basic Usage
2425
@@ -730,6 +731,207 @@ function isValidFileType(file: File, allowedTypes: string[]): boolean {
730731}
731732` ` `
732733
734+ ## FormData API
735+
736+ The FormData interface provides a way to construct key/value pairs representing form fields and their values for form submissions and multipart data handling.
737+
738+ ### Creating FormData
739+
740+ ` ` ` typescript
741+ // Create an empty FormData
742+ const formData = new FormData ();
743+
744+ // Append different types of data
745+ formData .append (" username" , " john_doe" );
746+ formData .append (" email" , " john@example.com" );
747+
748+ // Append a file
749+ const file = new File ([" file content" ], " document.txt" , { type: " text/plain" });
750+ formData .append (" document" , file );
751+
752+ // Append a Blob
753+ const blob = new Blob ([" binary data" ], { type: " application/octet-stream" });
754+ formData .append (" data" , blob , " data.bin" );
755+ ` ` `
756+
757+ ### FormData Methods
758+
759+ #### ` append (name , value , filename ? )`
760+
761+ Appends a new value to an existing key, or adds the key/value pair if it doesn't exist.
762+
763+ ` ` ` typescript
764+ const formData = new FormData ();
765+
766+ // Append string values
767+ formData .append (" name" , " John" );
768+ formData .append (" age" , " 30" );
769+
770+ // Append files with optional filename
771+ formData .append (" profile" , file , " profile.jpg" );
772+
773+ // Multiple values for the same key
774+ formData .append (" hobby" , " reading" );
775+ formData .append (" hobby" , " gaming" );
776+ ` ` `
777+
778+ #### ` set (name , value , filename ? )`
779+
780+ Sets a new value for an existing key, or adds the key/value pair if it doesn't exist. Unlike ` append ()` , this replaces existing values.
781+
782+ ` ` ` typescript
783+ formData .set (" name" , " Jane" ); // Replaces any existing "name" values
784+ formData .set (" avatar" , avatarFile , " avatar.png" );
785+ ` ` `
786+
787+ #### ` get (name )`
788+
789+ Returns the first value associated with the given name.
790+
791+ ` ` ` typescript
792+ const name = formData .get (" name" ); // Returns string or File
793+ console .log (" Name:" , name );
794+
795+ const file = formData .get (" document" ); // Returns File object if it was a file
796+ if (file instanceof File ) {
797+ console.log(" File name:" , file.name);
798+ }
799+ ` ` `
800+
801+ #### ` getAll (name )`
802+
803+ Returns all values associated with the given name.
804+
805+ ` ` ` typescript
806+ const hobbies = formData .getAll (" hobby" ); // Returns array of values
807+ console .log (" Hobbies:" , hobbies ); // ["reading", "gaming"]
808+ ` ` `
809+
810+ #### ` has (name )`
811+
812+ Returns whether a FormData object contains a certain key.
813+
814+ ` ` ` typescript
815+ if (formData .has (" email" )) {
816+ console.log(" Email field is present" );
817+ }
818+ ` ` `
819+
820+ #### ` delete(name)`
821+
822+ Deletes a key and all its values from the FormData object.
823+
824+ ` ` ` typescript
825+ formData.delete(" temp_field" );
826+ ` ` `
827+
828+ #### Iteration Methods
829+
830+ FormData supports iteration through its entries:
831+
832+ ` ` ` typescript
833+ // Iterate over all entries
834+ for (const [key, value] of formData.entries()) {
835+ console.log(` ${key }: ` , value);
836+ }
837+
838+ // Get all keys
839+ for (const key of formData .keys ()) {
840+ console.log(" Key:" , key);
841+ }
842+
843+ // Get all values
844+ for (const value of formData .values ()) {
845+ console.log(" Value:" , value);
846+ }
847+
848+ // Convert to array
849+ const entries = Array .from (formData .entries ());
850+ ` ` `
851+
852+ ### FormData with Fetch
853+
854+ FormData is commonly used with the Fetch API for submitting forms:
855+
856+ ` ` ` typescript
857+ async function submitForm() {
858+ const formData = new FormData ();
859+
860+ formData .append (" username" , " user123" );
861+ formData .append (" profile_pic" , profileFile );
862+ formData .append (" description" , " User profile update" );
863+
864+ try {
865+ const response = await fetch (" /api/profile" , {
866+ method: " POST" ,
867+ body: formData , // Browser automatically sets Content-Type: multipart/form-data
868+ });
869+
870+ if (response .ok ) {
871+ console .log (" Profile updated successfully" );
872+ }
873+ } catch (error ) {
874+ console .error (" Upload failed:" , error );
875+ }
876+ }
877+ ` ` `
878+
879+ ### Advanced FormData Usage
880+
881+ ` ` ` typescript
882+ // Building form data from an HTML form
883+ function createFormDataFromForm(form : HTMLFormElement ): FormData {
884+ const formData = new FormData ();
885+
886+ const inputs = form .querySelectorAll (' input, select, textarea' );
887+ inputs .forEach ((input ) => {
888+ if (input .type === ' file' ) {
889+ const files = input .files ;
890+ if (files ) {
891+ for (let i = 0 ; i < files .length ; i ++ ) {
892+ formData .append (input .name , files [i ]);
893+ }
894+ }
895+ } else if (input .type === ' checkbox' ) {
896+ if (input .checked ) {
897+ formData .append (input .name , input .value );
898+ }
899+ } else {
900+ formData .append (input .name , input .value );
901+ }
902+ });
903+
904+ return formData ;
905+ }
906+
907+ // Cloning FormData
908+ function cloneFormData(original : FormData ): FormData {
909+ const clone = new FormData ();
910+
911+ for (const [key, value] of original .entries ()) {
912+ clone .append (key , value );
913+ }
914+
915+ return clone ;
916+ }
917+
918+ // Converting FormData to URL-encoded string
919+ function formDataToUrlEncoded(formData : FormData ): string {
920+ const params = new URLSearchParams ();
921+
922+ for (const [key, value] of formData .entries ()) {
923+ if (typeof value === ' string' ) {
924+ params .append (key , value );
925+ } else {
926+ // For File/Blob objects, use their name or convert to string
927+ params .append (key , value .name || value .toString ());
928+ }
929+ }
930+
931+ return params .toString ();
932+ }
933+ ` ` `
934+
733935## Notes
734936
735937- File objects are immutable once created
0 commit comments