this line of code worked for me var blob = new Blob([new Uint8Array(BYTEARRAY)], { type: 'video/mp4' });

Answer from user2703473 on Stack Overflow
🌐
GitHub
gist.github.com › jdnichollsc › 78a6eb093731cf3e8dfd536dbe4befb3
Convert to Blob with Javascript · GitHub
Convert to Blob with Javascript. GitHub Gist: instantly share code, notes, and snippets.
Discussions

arrays - converting ByteArray into blob using javascript - Stack Overflow
I'm using flash to capture audio, encode it to mp3, then send it to javascript as ByteArray. Now I want the javascript to save it as MP3 on my computer (not flash saves it to my computer). I am using Blob and then getDataURL, but the file isn't playing when saved. More on stackoverflow.com
🌐 stackoverflow.com
javascript - Converting byte array output into Blob corrupts file - Stack Overflow
You should take the byte array and throw it into the blob constructor, turning a binary blob to string in javascript is a bad idea that can lead to "out of range" error or incorrect encoding More on stackoverflow.com
🌐 stackoverflow.com
How to convert byte array to blob in javascript for angular? - Stack Overflow
I am getting excel file from backend in the form of byte array. I want to convert that byte array into blob and then into file type. Please take a look at following code which i have used. this.get... More on stackoverflow.com
🌐 stackoverflow.com
html - byte array to blob javascript - Stack Overflow
I'm working on a windows8 machine, using IIS 8 and .NET 4.5. I've created a WCF restful service that returns a BLOB as a JSON string byte[]. On my client side i get the images and i try to read th... More on stackoverflow.com
🌐 stackoverflow.com
November 13, 2013
🌐
CodingTechRoom
codingtechroom.com › question › convert-byte-array-to-blob
How to Convert a Byte Array to a Blob in JavaScript - CodingTechRoom
// Sample byte array (example: representing text 'Hello') const byteArray = new Uint8Array([72, 101, 108, 108, 111]); // Convert byte array to Blob const blob = new Blob([byteArray], {type: 'text/plain'}); // Log the blob object to check its properties console.log(blob);
🌐
Stack Overflow
stackoverflow.com › questions › 70077556 › how-to-convert-byte-array-to-blob-in-javascript-for-angular › 70078891
How to convert byte array to blob in javascript for angular? - Stack Overflow
function(ext) { if (ext != undefined) { return this.extToMimes(ext); } return undefined; } extToMimes(ext) { let type = undefined; switch (ext) { case 'jpg': case 'png': case 'jpeg': type = 'image/jpeg' break; case 'txt': type = 'text/plain' break; case 'xls': type = 'application/vnd.ms-excel' break; case 'doc': type = 'application/msword' break; case 'xlsx': type = 'application/vnd.ms-excel' break; default: } return type; } let _type = this.function(fileExtension.toLowerCase()); const blob = new Blob([byteArray], { type: _type }); let file=new File([this.pdfResult],"sample.xlsx")
🌐
Angularfix
angularfix.com › 2022 › 02 › how-to-convert-byte-array-to-blob-in.html
How to convert byte array to blob in javascript for angular? ~ angularfix
May 2, 2022 - function(ext) { if (ext != undefined) { return this.extToMimes(ext); } return undefined; } extToMimes(ext) { let type = undefined; switch (ext) { case 'jpg': case 'png': case 'jpeg': type = 'image/jpeg' break; case 'txt': type = 'text/plain' break; case 'xls': type = 'application/vnd.ms-excel' break; case 'doc': type = 'application/msword' break; case 'xlsx': type = 'application/vnd.ms-excel' break; default: } return type; } let _type = this.function(fileExtension.toLowerCase()); const blob = new Blob([byteArray], { type: _type }); let file=new File([this.pdfResult],"sample.xlsx") Answered By - ABC Master
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 19949857 › byte-array-to-blob-javascript
html - byte array to blob javascript - Stack Overflow
November 13, 2013 - I'm working on a windows8 machine, using IIS 8 and .NET 4.5. I've created a WCF restful service that returns a BLOB as a JSON string byte[]. On my client side i get the images and i try to read th...
🌐
Rip Tutorial
riptutorial.com › converting between blobs and arraybuffers
JavaScript Tutorial => Converting between Blobs and ArrayBuffers
var blob = new Blob(["\x01\x02\x03\x04"]); var arrayPromise = new Promise(function(resolve) { var reader = new FileReader(); reader.onloadend = function() { resolve(reader.result); }; reader.readAsArrayBuffer(blob); }); arrayPromise.then(function(array) { console.log("Array contains", array.byteLength, "bytes."); }); var array = new Uint8Array([0x04, 0x06, 0x07, 0x08]); var blob = new Blob([array]); PDF - Download JavaScript for free · Previous Next · SUPPORT & PARTNERS · Advertise with us · Contact us · Cookie Policy · Privacy Policy · STAY CONNECTED · Get monthly updates about new articles, cheatsheets, and tricks. Cookie · This website stores cookies on your computer. We use cookies to enhance your experience on our website and deliver personalized content.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Blob › bytes
Blob: bytes() method - Web APIs - MDN Web Docs
September 5, 2024 - The bytes() method of the Blob interface returns a Promise that resolves with a Uint8Array containing the contents of the blob as an array of bytes. ... A Promise that fulfills with a Uint8Array object containing the blob data. The method will reject the returned Promise if, for example, the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Blob › arrayBuffer
Blob: arrayBuffer() method - Web APIs - MDN Web Docs
December 2, 2023 - A promise that resolves with an ArrayBuffer that contains the blob's data in binary form. While this method doesn't throw exceptions, it may reject the promise. This can happen, for example, if the reader used to fetch the blob's data throws an exception.
🌐
JavaScript.info
javascript.info › tutorial › binary data, files
Blob
May 16, 2022 - We can make a Blob from a typed array using new Blob(...) constructor.
🌐
Bun
bun.com › docs › guides › binary › arraybuffer-to-blob
Convert an ArrayBuffer to a Blob - Bun
3 weeks ago - By default the type of the resulting Blob is unset. Set it with the type option. const buf = new ArrayBuffer(64); const blob = new Blob([buf], { type: "application/octet-stream" }); blob.type; // => "application/octet-stream"
Top answer
1 of 7
162

You can use FileReader to read the Blob as an ArrayBuffer.

Here's a short example:

var arrayBuffer;
var fileReader = new FileReader();
fileReader.onload = function(event) {
    arrayBuffer = event.target.result;
};
fileReader.readAsArrayBuffer(blob);

Here's a longer example:

// ArrayBuffer -> Blob
var uint8Array  = new Uint8Array([1, 2, 3]);
var arrayBuffer = uint8Array.buffer;
var blob        = new Blob([arrayBuffer]);

// Blob -> ArrayBuffer
var uint8ArrayNew  = null;
var arrayBufferNew = null;
var fileReader     = new FileReader();
fileReader.onload  = function(event) {
    arrayBufferNew = event.target.result;
    uint8ArrayNew  = new Uint8Array(arrayBufferNew);

    // warn if read values are not the same as the original values
    // arrayEqual from: http://stackoverflow.com/questions/3115982/how-to-check-javascript-array-equals
    function arrayEqual(a, b) { return !(a<b || b<a); };
    if (arrayBufferNew.byteLength !== arrayBuffer.byteLength) // should be 3
        console.warn("ArrayBuffer byteLength does not match");
    if (arrayEqual(uint8ArrayNew, uint8Array) !== true) // should be [1,2,3]
        console.warn("Uint8Array does not match");
};
fileReader.readAsArrayBuffer(blob);
fileReader.result; // also accessible this way once the blob has been read

This was tested out in the console of Chrome 27—69, Firefox 20—60, and Safari 6—11.

Here's also a live demonstration which you can play with: https://jsfiddle.net/potatosalad/FbaM6/

Update 2018-06-23: Thanks to Klaus Klein for the tip about event.target.result versus this.result

Reference:

  • https://developer.mozilla.org/en-US/docs/Web/API/FileReader#readAsArrayBuffer()
  • https://www.w3.org/TR/FileAPI/#dfn-readAsArrayBuffer
2 of 7
77

The Response API consumes a (immutable) Blob from which the data can be retrieved in several ways. The OP only asked for ArrayBuffer, and here's a demonstration of it.

var blob = GetABlobSomehow();

// NOTE: you will need to wrap this up in a async block first.
/* Use the await keyword to wait for the Promise to resolve */
const arrayBuffer = await new Response(blob).arrayBuffer();

Alternatively you could use this:

new Response(blob).arrayBuffer().then((arrayBuffer) => {
 // do something with the arrayBuffer
});

Note: This API isn't compatible with older (ancient) browsers so take a look to the Browser Compatibility Table to be on the safe side ;)

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Blob
Blob - Web APIs - MDN Web Docs
To obtain a Blob object for a file on the user's file system, see the File documentation. The APIs accepting Blob objects are also listed in the File documentation. ... Returns a newly created Blob object which contains a concatenation of all of the data in the array passed into the constructor. ... The size, in bytes...
🌐
Microsoft
social.msdn.microsoft.com › Forums › en-US › 566de775-d9e2-4978-bf10-64e001fe7872 › convert-blob-to-bytearray
Convert Blob to ByteArray | Microsoft Learn
August 3, 2021 - var byteArray = new Uint8Array... byte in the array } ... Blob blob = rs.getBlob("SomeDatabaseField"); int blobLength = (int) blob.length(); byte[] blobAsBytes = blob.getBytes(1, blobLength); ... Is this Javascript ...
🌐
Reddit
reddit.com › r/javascript › [askjs] suddenly blob creation from large typed array is no longer allowed?
r/javascript on Reddit: [AskJS] Suddenly blob creation from large typed array is no longer allowed?
July 4, 2024 -

This is just a demo code snippet and its variations are working perfectly for more than a year until now.

I suspect the recent browser updates for Edge 126.0.2592.87 (Official build) (64-bit) and Chrome 126.0.6478.127 (Official Build) (64-bit) breaks it, because they used the same underlying engine. Firefox seems to be still okay.

var myArray = new Uint8Array(1024 * 1024 * 1024 * 1.8);
var myBlob = new Blob([myArray], {type: "*.*"});
myBlob.slice(0, 10).arrayBuffer();

If the code snippet works in the first run then rerun it twice without refreshing browser. You will be greeted with the message "Uncaught DOMException: The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired."

Any thoughts or insights? Appreciated.

Top answer
1 of 2
11
You are trying to create a ~2 GB TypedArray, then create a ~2 GB Blob. Then create a 10 byte Blob, then a 10 byte ArrayBuffer. That's ~4 GB of RAM. In 3 lines of code. You are probably running out of RAM. Blob storage is not contigous in Chrome. See Where is Blob binary data stored? . You really don't need to create the Blob at all, unless you are using stream() method or creating an Blob URL for some programming reasons. Just use subarray() method of TypedArray to get 10 bytes from the ~2 GB underlying ArrayBuffer source of the initial Uint8Array you create. The type "*.*" is not a valid MIME type. You should probably use "application/octet-stream" or just "text/plain" for binary data. 1024 * 1024 * 1024 * 1.8 outputs a float, 1932735283.2. You probably want to use Math.ceil(1024 * 1024 * 1024 * 1.8) to round up.
2 of 2
2
I'm pretty sure this doesn't have anything to do with Chrome versions but with your free space (RAM and Disk). // CrOS: // * Ram - 20% // * Disk - 50% // Note: The disk is the user partition, so the operating system can still // function if this is full. // Android: // * RAM - 1% // * Disk - 6% // Desktop: // * Ram - 20%, or 2 GB if x64. // * Disk - 10% This is a comment on the function used by Chrome to estimate the amount of Blob storage limits. Usually a Blob is read/write from disk when the memory (RAM) pressure is high so Chrome tries to write the blob to disk, but only if there's enough free space in the disk (otherwise this could lead to potential securiity issues). As you can see there's a max limit for Blobs of 2GB (on x64) but this should not be the problem because you're allocating 1.8GB. If you want to take a look at the source code: https://source.chromium.org/chromium/chromium/src/+/main:storage/browser/blob/blob_memory_controller.cc;l=75;drc=d311b843fa6d3cfd69f650d05cd44ace6aaa968d;bpv=1;bpt=1