Skip to content

Instantly share code, notes, and snippets.

@apacheli
Created August 5, 2022 00:13
Show Gist options
  • Select an option

  • Save apacheli/013797e914623cf874260951c798e936 to your computer and use it in GitHub Desktop.

Select an option

Save apacheli/013797e914623cf874260951c798e936 to your computer and use it in GitHub Desktop.
export interface Field {
contentType?: string;
filename?: string;
name: string;
value: string | Uint8Array;
}
export const boundary = Math.random().toString(16).substring(2);
export async function* fileIterator(fields: Field[]) {
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
let str = `Content-Disposition:form-data;name=${field.name}`;
if (field.filename !== undefined) {
str += `;filename=${field.filename}`;
}
if (field.contentType !== undefined) {
str += `;Content-Type:${field.contentType}`;
}
yield `\n--${boundary}\n${str}\n\n`;
yield field.value;
}
yield `\n--${boundary}--`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment