How to convert blob to base64 encoding using JavaScript ? Last Updated : 24 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Blob in JavaScript is a fundamental data type representing bytes of data. It serves as the underlying structure for files and is supported by web browsers, capable of storing and retrieving data from memory. Blobs have specific sizes and file types like regular files and can be handled with Buffers, ideal for binary data storage such as images or files. Using the FileReader API, Blobs can be converted into Base64 encoded strings. This encoding is essential for transferring binary data over networks, ensuring compatibility across different protocols and reducing the risk of corruption during transmission. Using file reader with readAsDataURL() methodThe 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. Example: To demonstrate converting blob to Base64 with FileReader API. 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: Using FileReader with readAsArrayBuffer()Using 'FileReader' with 'readAsArrayBuffer()' allows converting data from a Blob into an ArrayBuffer, facilitating efficient manipulation and transformation of binary data in JavaScript applications. Example: To demonstrate converting blob to base 64 encoding using JavaScript fileReader with readAsArrayBuffer() method. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Convert Blob to Base64 String</title> </head> <body> <script type="text/javascript"> // Creating a Blob with text content let blob = new Blob(["Hello, world!"], { type: "text/plain" }); // Creating a FileReader instance let reader = new FileReader(); // Reading the Blob as an ArrayBuffer reader.readAsArrayBuffer(blob); // Event handler for when the FileReader has finished loading reader.onloadend = function () { // Obtaining the ArrayBuffer let buffer = reader.result; // Converting ArrayBuffer to Base64 string let base64data = btoa(String.fromCharCode.apply(null, new Uint8Array(buffer))); // Logging the Base64 encoded string console.log('Base64 encoded:', base64data); } </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to convert blob to base64 encoding using JavaScript ? radheshkhanna Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc JavaScript-Questions Similar Reads How to Convert Base64 to Blob in JavaScript? Working with files and data in web applications often involves dealing with binary data. One common scenario is converting a Base64 string into a Blob object, which can then be used in various ways, such as creating downloadable files or uploading images to a server. This article will guide you thro 4 min read How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim 2 min read How to convert image into base64 string using JavaScript ? In this article, we will convert an image into a base64 string using Javascript. The below approaches show the methods to convert an image into a base64 string using Javascript.ApproachHere we will create a gfg.js file which will include JavaScript code and one gfg.html file.Now we will put onchange 2 min read How to Convert JSON to base64 in JavaScript ? Base 64 is the encoding scheme that represents binary data in a printable ASCII format, commonly used for data serialization and transmission. Table of Content Using btoa functionUsing Manual ConversionUsing btoa functionIn this approach, we're using btoa to encode a UTF-8 string representation of a 2 min read How To Convert Base64 to JSON String in JavaScript? There could be situations in web applications, where there is a need to decode the data from Base64 format back into its original JSON format. It generally happens when one has to transmit data over the network where Base64 encoding is well suited for encoding binary data.In this article, we will se 2 min read How to Convert JSON to Blob in JavaScript ? This article explores how to convert a JavaScript Object Notation (JSON) object into a Blob object in JavaScript. Blobs represent raw data, similar to files, and can be useful for various tasks like downloading or processing JSON data. What is JSON and Blob?JSON (JavaScript Object Notation): A light 2 min read How to Convert Blob Data to JSON in JavaScript ? When dealing with Blob data in JavaScript, such as binary data or files, we may need to convert it into JSON format for doing so JavaScript provides us with various methods as listed below. Table of Content Using FileReader APIUsing TextDecoder APIUsing FileReader APIIn this approach, we first use t 2 min read JavaScript | Encode/Decode a string to Base64 To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a- 6 min read How to convert decimal to octal in JavaScript ? In this article, we are given a number and the task is to convert the number from decimal to octal. This can be done by using number.toString(8) method. It takes the parameter which is the base of the converted string. In this case, the base will be 8. Syntax: number.toString(8) The below examples s 2 min read How to Convert Excel to JSON in JavaScript ? Converting Excel spreadsheets to JSON format is a common requirement in various applications. JavaScript code utilizes the read-excel-file library to parse the Excel data, convert it to JSON format, and display it. Additionally, it provides functionality to download the generated JSON file. Approach 3 min read Like