Skip to content

Commit 3345d4a

Browse files
committed
Create Multipart from Blob
1 parent bbceca3 commit 3345d4a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Multipart.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ export class Multipart implements Part {
231231
return Multipart.parseBody(part.bytes(), new TextEncoder().encode(boundary), mediaType ?? void 0);
232232
}
233233

234+
/**
235+
* Create Multipart from a {@link !Blob}. The boundary and media type are determined from the blob's type.
236+
* @param blob The blob
237+
* @throws {@link !SyntaxError} If the `Content-Type` header is missing or does not include a boundary
238+
*/
239+
public static async blob(blob: Blob): Promise<Multipart> {
240+
const type = blob.type;
241+
if (type === "") throw new SyntaxError("Blob is missing Content-Type header");
242+
const {mediaType, boundary} = Multipart.parseContentType(type);
243+
if (boundary === null) throw new SyntaxError("Missing boundary in Content-Type header of blob");
244+
return Multipart.parseBody(new Uint8Array(await blob.arrayBuffer()), new TextEncoder().encode(boundary), mediaType ?? void 0);
245+
}
246+
234247
/**
235248
* Create Multipart from {@link FormData}.
236249
* This method might be slow if the form data contains large files.

0 commit comments

Comments
 (0)