this line of code worked for me var blob = new Blob([new Uint8Array(BYTEARRAY)], { type: 'video/mp4' });
Answer from user2703473 on Stack Overflowarrays - converting ByteArray into blob using javascript - Stack Overflow
javascript - Converting byte array output into Blob corrupts file - Stack Overflow
How to convert byte array to blob in javascript for angular? - Stack Overflow
html - byte array to blob javascript - Stack Overflow
There is probably an easier way to do this, but this works in IE and Chrome.
- First, I converted the byte array to base64.
- Next I converted the base64 to a Uint8Array.
- Then I display the file.
Here is the code that worked for me:
lwsService.getdocument(id)
.success(function (response) {
var byteArray = new Uint8Array(response[0].binFileImage);
var blob = new Blob([byteArray], { type: 'application/pdf' });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob);
} else {
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}
I hope this helps someone else.
Try the following example. it's using FileSaver.
var blob = new Blob([content], {type: 'application/octet-stream'});
saveAs(blob, "yourFile.pdf");
I ended up doing this with the fileContent string:
let bytes = new Uint8Array(fileContent.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = fileContent.charCodeAt(i);
}
I then proceed to build the Blob with these bytes:
let blob = new Blob([bytes], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
If I then send this via a POST request, the file isn't mangled and can be opened correctly by Word.
I still get the feeling this can be achieved with less hassle / less steps. If anyone has a better solution, I'd be very interested to learn.
thx for your answer, Uint8Array was the solution. Just a little improvement, to avoid creating the string:
let bytes = new Uint8Array(docdata.length);
for (var i = 0; i < docdata.length; i++) {
bytes[i] = docdata[i];
}
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
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 ;)
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.