Blob API
This lesson explains Blob API with clear examples, use cases, and best practices so you can use HTML5 APIs confidently in real web applications.
HTML5 Blob API Overview
The HTML5 Blob API allows developers to create, store, process, preview,
and download raw binary data directly in the browser. A
Blob represents immutable file-like data and is commonly used
for generating files, handling downloads, previewing media, processing API
responses, and working with client-side data.
Blob stands for Binary Large Object. It can contain text,
images, audio, video, JSON, CSV, PDFs, or any other binary data. Combined
with URL.createObjectURL(), FileReader, and
Fetch API, Blob makes browser-based file workflows powerful
and flexible.
| Feature | Description |
| API Name | Blob API |
| Main Object | Blob |
| Data Type | Immutable binary or text data. |
| Common Methods | slice(), text(), arrayBuffer(), stream() |
| Common Use Cases | Downloads, previews, file generation, API responses. |
| Works With | File API, FileReader, Fetch API, Object URLs. |
Basic Blob API Example
// HTML5 Blob API
const content = "Hello from the Blob API!";
const blob = new Blob(
[content],
{ type: "text/plain" }
);
console.log(blob.size);
console.log(blob.type);
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "message.txt";
link.click();
URL.revokeObjectURL(url);
This example creates a text file in memory, generates a temporary object
URL, downloads the file, and then releases the URL to avoid memory leaks.
Blob Constructor Syntax
The Blob() constructor accepts an array of data parts and an
optional configuration object that defines the MIME type.
| Syntax | Description |
| new Blob() | Creates a new Blob object. |
| [data] | Array containing text, binary data, ArrayBuffer, or other Blob values. |
| type | Defines the MIME type such as text/plain or application/json. |
| size | Returns the Blob size in bytes. |
| type property | Returns the MIME type assigned to the Blob. |
Blob Methods
| Method | Purpose | Example |
| text() | Reads Blob content as text. | blob.text() |
| arrayBuffer() | Reads Blob content as binary data. | blob.arrayBuffer() |
| stream() | Returns a readable stream. | blob.stream() |
| slice() | Creates a smaller Blob from part of another Blob. | blob.slice(0, 100) |
Common Blob API Use Cases
The Blob API is useful whenever a web application needs to create or
process file-like data without immediately sending it to a server.
| Use Case | How Blob Helps |
| Generate Downloads | Create TXT, CSV, JSON, XML, or PDF downloads in the browser. |
| Preview Files | Create temporary URLs for images, videos, audio, and PDFs. |
| API File Responses | Process downloaded files from Fetch responses. |
| Media Recording | Store audio or video chunks from MediaRecorder. |
| Offline Editors | Export edited content as downloadable files. |
| Binary Processing | Work with ArrayBuffer, streams, and binary data. |
Create JSON Blob Example
// Create and download a JSON file
const user = {
name: "Ayaan",
role: "Student",
active: true
};
const jsonBlob = new Blob(
[JSON.stringify(user, null, 2)],
{ type: "application/json" }
);
const jsonUrl = URL.createObjectURL(jsonBlob);
const link = document.createElement("a");
link.href = jsonUrl;
link.download = "user.json";
link.click();
URL.revokeObjectURL(jsonUrl);
This example converts a JavaScript object into JSON, creates a Blob, and
downloads it as a user.json file.
Fetch API with Blob Example
// Download file using Fetch and Blob
async function downloadFile() {
const response = await fetch("/files/report.pdf");
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "report.pdf";
link.click();
URL.revokeObjectURL(url);
}
downloadFile();
The Fetch API can convert server responses into Blob objects. This is
useful for downloading PDFs, images, spreadsheets, and other binary files.
Blob API Best Practices
- Always set the correct MIME type when creating a Blob.
- Use
URL.revokeObjectURL() after object URLs are no longer needed. - Use
blob.text() for text-based data. - Use
blob.arrayBuffer() for binary processing. - Avoid keeping large Blob objects in memory longer than necessary.
- Validate file content on the server when uploads are involved.
- Use clear file names when triggering downloads.
- Provide accessible labels and status messages for file actions.
Common Blob API Mistakes
- Forgetting to revoke object URLs, causing memory leaks.
- Using the wrong MIME type for generated files.
- Using Base64 data URLs for large files instead of Blob URLs.
- Reading large files fully into memory without user feedback.
- Assuming Blob data is automatically saved to disk.
- Not handling errors when downloading or processing files.
- Using Blob downloads without accessible status messages.
- Trusting client-generated file data without server validation.
Key Takeaways
- The Blob API represents immutable file-like data in the browser.
- Blob can store text, JSON, images, media, PDFs, and binary data.
- Use
URL.createObjectURL() to preview or download Blob data. - Always revoke object URLs after use to prevent memory leaks.
- Blob works well with Fetch, FileReader, File API, and MediaRecorder.
- The Blob API is essential for modern file handling in web applications.
Pro Tip
Use Blob URLs instead of Base64 strings for large files. Blob URLs are
more memory-efficient, easier to download, and better suited for images,
videos, PDFs, CSV files, and API-generated documents in production web
applications.