Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
const blob = new Blob(["Hello, ", "world"], { type: "text/plain" });
const file = new File([blob], "hello.txt", { type: "text/plain" });

Try it

JS Build and read a Blob

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS File objects and JSON

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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() or arrayBuffer(); the older FileReader still works but is clumsier.
  • Object URLs. URL.createObjectURL(blob) returns a short-lived blob: URL for <img>, <a download> or <video>. It keeps the blob in memory until you call URL.revokeObjectURL(url) or the page closes, so revoke it when done.
  • Files from users. You only get a File when the user selects it. Never trust name or type; both are supplied by the user's system.
  • Uploading. Pass a blob or file in FormData or directly as a fetch() body.
  • Text encoding. Strings in parts are stored as UTF-8, and text() 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.