LazyFile
Summary
A lazy, streaming alternative to File.
Important: Since LazyFile is not a File subclass, you cannot pass it directly to APIs
that expect a real File (like new Response(file) or formData.append('file', file)).
Instead, use one of:
.stream()- Returns aReadableStreamforResponseand other streaming APIs.toFile()- Returns aPromise<File>for non-streaming APIs that require a completeFile(e.g.FormData).toBlob()- Returns aPromise<Blob>for non-streaming APIs that require a completeBlob(e.g.FormData)
Signature
class LazyFile {
constructor(
parts: LazyContent | BlobPartLike[],
name: string,
options: LazyFileOptions,
): LazyFile;
// Properties
lastModified: number;
name: string;
webkitRelativePath: "";
// Accessors
get [toStringTag](): string;
get size(): number;
get type(): string;
// Methods
arrayBuffer(): Promise<ArrayBuffer>;
bytes(): Promise<Uint8Array<ArrayBuffer>>;
slice(start: number, end: number, contentType: string): LazyBlob;
stream(): ReadableStream<Uint8Array<ArrayBuffer>>;
text(): Promise<string>;
toBlob(): Promise<Blob>;
toFile(): Promise<File>;
toString(): never;
}
Constructor Params
parts
The file parts or lazy content
name
The name of the file
options
Options for the file
Properties
lastModified
The last modified timestamp of the file in milliseconds.
name
The name of the file.
webkitRelativePath
Always empty string. This property exists only for structural compatibility with the native
File interface. It's a browser-specific property for files selected via <input type="file">
with the webkitdirectory attribute, which doesn't apply to programmatically created files.
Accessors
[toStringTag]
The brand string exposed by Object.prototype.toString.call().
size
The size of the file in bytes.
type
The MIME type of the file.
Methods
arrayBuffer(): Promise
Returns the file's content as an ArrayBuffer.
bytes(): Promise<Uint8Array>
Returns the file's contents as a byte array.
slice(start: number, end: number, contentType: string): LazyBlob
Returns a new LazyBlob that contains the data in the specified range.
Note: Like the native File.slice(), this returns a Blob (not a File).
start
The start index (inclusive)
end
The end index (exclusive)
contentType
The content type of the new blob
stream(): ReadableStream<Uint8Array>
Returns a stream that can be used to read the file's contents.
text(): Promise
Returns the file's contents as a string.
toBlob(): Promise
Converts this LazyFile to a native Blob.
Warning: This reads the entire content into memory, which defeats the purpose of using
a lazy file for large files. Only use this for non-streaming APIs that require a complete Blob.
Use .stream() to get a ReadableStream for Response and other streaming APIs.
toFile(): Promise
Converts this LazyFile to a native File.
Warning: This reads the entire content into memory, which defeats the purpose of using
a lazy file for large files. Only use this for non-streaming APIs that require a complete File
(e.g. FormData). For streaming, use .stream() instead.