What is blob in JavaScript
What is a Blob?
In programming, especially JavaScript, you might have come across the term 'Blob'. Blob stands for Binary Large Object and is used to represent data that isn't necessarily in a JavaScript-native format. In simpler terms, blobs are used to handle different types of data like images, zip files, audio files, etc.
Blob: A Bag of Data
Imagine you're packing for a trip. You have different types of items - clothes, toiletries, electronic gadgets, etc. You can't fit them all in your backpack the way they are, can you? So, you pack them in different ziplock bags. Here, the blob is like the ziplock bag that holds your data (items for the trip).
Creating a Blob
Creating a blob in JavaScript is quite simple. Here's how you do it:
let blob = new Blob(["Hello, world!"], {type : 'text/plain'});
In this example, we're creating a new Blob object. The object takes two arguments - the data and an options object. The data is an array of BlobPart, which can be a Blob, a String, an ArrayBuffer, or an ArrayBufferView. The options object can contain the type of the Blob and the endings (either 'transparent' or 'native').
Blob Size and Type
Once you have a blob, you can check its size and type using the size
and type
properties. Here's how you do it:
let blob = new Blob(["Hello, world!"], {type : 'text/plain'});
console.log(blob.size);
console.log(blob.type);
In this example, blob.size
will return the size of the blob in bytes, and blob.type
will return the MIME type of the blob, which is 'text/plain' in this case.
Using Blobs
Blobs are mainly used for two purposes - to download data and to upload data.
Downloading Data
If you want to create a downloadable link for your blob, you can use the URL.createObjectURL()
method. This method creates a simple URL string that represents your blob. Here's how you do it:
let blob = new Blob(["Hello, world!"], {type : 'text/plain'});
let url = URL.createObjectURL(blob);
console.log(url);
In this example, url
will be a string like 'blob:https://your-site.com/uuid'. You can use this URL as the href attribute for a download link.
Uploading Data
If you want to upload a blob to a server, you can use the FormData
API. Here's how you do it:
let blob = new Blob(["Hello, world!"], {type : 'text/plain'});
let formData = new FormData();
formData.append('file', blob, 'filename.txt');
fetch('/upload', {method: 'POST', body: formData});
In this example, we're creating a new FormData object and appending our blob as a 'file'. The 'filename.txt' is the filename that the server will see. Then we're using the fetch
API to send a POST request to the '/upload' endpoint.
Conclusion
Just like packing for a trip, handling data can be a task in programming. But blobs make it easier. They allow you to handle different types of data in a uniform way. So next time you come across a situation where you need to handle data that's not in a JavaScript-native format, just create a blob, put your data in it, and handle it like any other JavaScript object. Happy packing, or rather, happy blobbing!