JavaScript Reference ยท Typed Arrays and Binary Data
Blob and File
Reference for the JavaScript Blob and File objects: build binary data from parts, read it as text or bytes, slice it, and create downloadable object URLs.
A Blob is an immutable chunk of binary data with a size and a MIME type. You build one from strings, typed arrays, buffers or other blobs, and read it back asynchronously. A File is a Blob with a name and a lastModified time; the browser gives you File objects from <input type="file">, drag and drop and the File System Access API, and you can create your own.
const blob = new Blob(["Hello, ", "world"], { type: "text/plain" }); const file = new File([blob], "hello.txt", { type: "text/plain" });
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
new Blob(parts, options) new File(parts, name, options) blob.size blob.type blob.slice(start, end, contentType) blob.text() blob.arrayBuffer() blob.bytes() blob.stream() URL.createObjectURL(blob) URL.revokeObjectURL(url)
| Name | Description |
|---|---|
parts |
An array of strings, ArrayBuffers, typed arrays, DataViews or other blobs, joined in order. |
options.type |
The MIME type (lowercased); empty string by default. |
options.endings |
"transparent" (default) or "native", which converts line endings in string parts. |
options.lastModified |
File only: milliseconds since the epoch. Defaults to now. |
name |
File only: the file name. |
start, end |
Byte range for slice(); negative values count from the end. |
contentType |
Optional MIME type for the slice. |
Return value: text() resolves to a string (decoded as UTF-8), arrayBuffer() to an ArrayBuffer, bytes() to a Uint8Array (newer), stream() returns a ReadableStream, slice() returns a new Blob synchronously.
Properties: Blob: size, type. File adds name, lastModified and, for files from a directory picker, webkitRelativePath.
Usage notes
- Immutable. A blob never changes.
slice()creates a new blob, usually without copying the data. - Reading is asynchronous. Await
text()orarrayBuffer(); the olderFileReaderstill works but is clumsier. - Object URLs.
URL.createObjectURL(blob)returns a short-livedblob:URL for<img>,<a download>or<video>. It keeps the blob in memory until you callURL.revokeObjectURL(url)or the page closes, so revoke it when done. - Files from users. You only get a
Filewhen the user selects it. Never trustnameortype; both are supplied by the user's system. - Uploading. Pass a blob or file in
FormDataor directly as afetch()body. - Text encoding. Strings in
partsare stored as UTF-8, andtext()always decodes as UTF-8. - Size is in bytes, not characters.
Browser support
Blob and File work in all current browsers, and in Node.js. bytes() is a more recent addition, so check a compatibility table.