Syntax
const blob = new Blob(["Hello"], { type: "text/plain" });
const url = URL.createObjectURL(blob);
URL.revokeObjectURL(url);Examples
Binary Data with Blob and Object URLs: working example
Run this standalone example in a modern browser console unless another environment is specified. Expected result: In a browser supporting Blob URLs, the console prints Hello Ada 10.
(async () => {
const blob = new Blob(["Hello Ada\n"], { type: "text/plain;charset=utf-8" });
const url = URL.createObjectURL(blob);
try {
const response = await fetch(url);
console.log((await response.text()).trim(), blob.size);
} finally {
URL.revokeObjectURL(url);
}
})();- Expected result: In a browser supporting Blob URLs, the console prints Hello Ada 10.
- The Blob contains nine ASCII text characters plus a newline, totaling ten bytes.
- finally releases the URL after its asynchronous reader completes.
Best practices
- Do not revoke an object URL before an image, download, or fetch has consumed it.
- Store the underlying content when persistence is required.
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification