Blob  is a fundamental data type in JavaScript. Blob stands for Binary Large Object  and it is a representation of bytes of data. Web browsers support the Blob data type for holding data. Blob is the underlying data structure for the File  object and the FileReader  API. Blob has a specific size and file type just like ordinary files and it can be stored and retrieved from the system memory. Blob can also be converted and read as Buffers . Buffers are very handy to store binary data such as the binary data of an image or a file. We will be using the FileReader  API to convert Blob to a Base64  Encoded String in JavaScript.
We cannot transfer Binary data over a Network in its raw format. This is because the raw bytes may be interpreted incorrectly due to the different protocols involved in the Network. There is also a higher chance of it being corrupted while being transferred over the Network. Hence this binary data is encoded into characters using Base64  encoding before being transferred over the network such as in email attachments, HTML form data, etc. Base64 encoding is a way of converting arbitrary Binary data into ASCII  characters. Base64 encoding is used so that we do not have to rely on external files and scripts in web browsers.
Example:  Convert Blob  to Base64  Encoded String using FileReader  API. The FileReader.readAsDataURL()  reads the contents of the specified Blob data type and will return a Base64  Encoded String with data:  attribute. The FileReader.onloadend  Event is fired when the reading of the data has been completed successfully or when an error is encountered. We have created a sample Blob using the Blob()  constructor. The constructor takes in values in a String[]  and an Object consisting of the String type .
Program:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Convert Blob to Base64 String</title>
</head>
<body>
<div>Hello GeeksForGeeks</div>
<script type="text/javascript">
let blob = new Blob(["GeeksForGeeks"],
{ type: "text/plain" });
// The full Blob Object can be seen
// in the Console of the Browser
console.log('Blob - ', blob);
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function () {
let base64String = reader.result;
console.log('Base64 String - ', base64String);
// Simply Print the Base64 Encoded String,
// without additional data: Attributes.
console.log('Base64 String without Tags- ',
base64String.substr(base64String.indexOf(', ') + 1));
}
</script>
</body>
</html>
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
14 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...