Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download Binary Files with Javascript

Tags:

javascript

I want to download binary files using Javascript.

I have a REST service that returns the binary data and i want to know if its possible to show the binary file, whichever the file extension.

This is my current code:

var xhr = new XMLHttpRequest; xhr.open("GET", requestUrl); xhr.addEventListener("load", function () {     var ret = [];     var len = this.responseText.length;     var byte;     for (var i = 0; i < len; i++) {         byte = (this.responseText.charCodeAt(i) & 0xFF) >>> 0;         ret.push(String.fromCharCode(byte));     }     var data = ret.join('');     data = "data:application/pdf;base64," + btoa(data);      window.open(data, '_blank', 'resizable, width=1020,height=600'); }, false);  xhr.setRequestHeader("Authorization", "Bearer " + client.accessToken); xhr.overrideMimeType("octet-stream; charset=x-user-defined;"); xhr.send(null); 

Thanks!

like image 398
nuno.filipesf Avatar asked Jul 17 '13 09:07

nuno.filipesf


People also ask

Is JavaScript binary file?

JavaScript doesn't have a "binary" type and so they went with a String with the guarantee that no character stored in the string would be outside the range 0.. 255. (They could have gone with an array of numbers instead, but they didn't.) The example shows how to get the raw value of a "character" from the string.

Can I send binary data over HTTP?

We can use the above multipart/form-data protocol to transfer the binary data through HTTP services. We can send the data in above format with some unique boundary string which can be easily parsed at client side.

What is binary data in JavaScript?

Binary data in JavaScript is implemented in a non-standard way, compared to other languages. But when we sort things out, everything becomes fairly simple. The basic binary object is ArrayBuffer – a reference to a fixed-length contiguous memory area.


1 Answers

Have a look at the MDN article on XMLHttpRequest.

If you set the response of the XMLHttpRequest to ArrayBuffer you could do the following:

var xhr = new XMLHttpRequest(); xhr.open("GET", requestUrl); xhr.responseType = "arraybuffer";  xhr.onload = function () {     if (this.status === 200) {         var blob = new Blob([xhr.response], {type: "application/pdf"});         var objectUrl = URL.createObjectURL(blob);         window.open(objectUrl);     } }; xhr.send(); 

Option 2:
You could use Blob as the response of the XMLHttpRequest. And then maybe save it in the FileSystem (FileSystem API)

It may look like:

var xhr = new XMLHttpRequest(); xhr.open("GET", requestUrl); xhr.responseType = "blob";  xhr.onload = function () {     onDownloaded(this); }; xhr.send(); 

Option 3:
If you only want to download and "show" images you can easily do this like so:

var img = new Image();  // add the onload event before setting the src img.onload = function() {     onImageDownloaded(img); }  // start the download by setting the src property img.src = requestUrl 
like image 92
Jo David Avatar answered Oct 02 '22 13:10

Jo David