Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery AJAX to download a binary file

I am attempting to use jQuery AJAX to download a binary audio file.

Normally I would just issue a command like this:

 windows.location.href = 'http://marksdomain(dot)com/audioFile.wav' ; 

However, recently our server has been waiting too long to respond, and I get a nasty gateway timeout message.

It has been suggested that I use jQuery AJAX instead, which makes sense since then i would have more control over the timeout.

Here is the code i have played with so far:

$.ajax({     url: 'http://marksdomain(dot)com/audioFile.wav',     timeout: 999999,     dataType: 'binary',     processData: false, // this one does not seem to do anything ?      success: function (result) {         console.log(result.length);     },     error: function (result, errStatus, errorMessage) {         console.log(errStatus + ' -- ' + errorMessage);     } }; 

When I omit the "dataType", the binary file is coming through about three times larger than it actually is on the server. However, when i make the dataType equal to "binary", AJAX throws an error:

"No conversion from text to binary" 

From some earlier posts, it sounds as if jQuery AJAX cannot handle binary files in this manner.

I did discover Delivery.js which actually works quite well for what I am attempting, but I would rather not use a node solution if possible.

Any suggestions?

like image 375
edwardsmarkf Avatar asked Nov 24 '15 19:11

edwardsmarkf


People also ask

Can we download file using AJAX?

You need to set it according to your file type. You can use this technique to download any kind of files. "We cannot download the file through Ajax, must use XMLHttpRequest". XMLHttpRequest is AJAX by definition.

How can download PDF with button click in jQuery?

Downloading PDF File on Button Click using jQueryInside the DownloadFile JavaScript function, the URL of the File is passed as parameter to the jQuery AJAX function. Inside the jQuery AJAX function, using the XmlHttpRequest (XHR) call, the PDF file is downloaded as Byte Array (Binary Data).

What is AJAX in JavaScript with example?

AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

Is not a function AJAX?

ajax is not a function" error occurs when loading the slim version of jQuery and trying to use the ajax function. The ajax function is excluded from the slim jQuery version. To solve the error load the regular jQuery version on your page.


1 Answers

Just use XHR directly. This example is taken from MDN:

var oReq = new XMLHttpRequest(); oReq.open("GET", "/myfile.png", true); oReq.responseType = "arraybuffer";  oReq.onload = function(oEvent) {   var arrayBuffer = oReq.response;    // if you want to access the bytes:   var byteArray = new Uint8Array(arrayBuffer);   // ...    // If you want to use the image in your DOM:   var blob = new Blob([arrayBuffer], {type: "image/png"});   var url = URL.createObjectURL(blob);   someImageElement.src = url;    // whatever... };  oReq.send(); 
like image 59
Brandon Avatar answered Oct 08 '22 08:10

Brandon