4

I want to downlaod exe file using $http, it shows me when I console the data, but I am unable to download it.

$http({
        url: url,
        method: "GET",
        headers: {
           'Content-type': 'application/json'
        }
    }).success(function (data, status, headers, config) {
        console.log(data);

        window.open(objectUrl);
    }).error(function (data, status, headers, config) {
        //upload failed
    });

Any help would be appreciated.

2 Answers 2

4

you can Use response type like responseType: "arraybuffer"

$http({
    url: url,
    method: "GET",
    headers: {
       'Content-type': 'application/json'
    },
  responseType: "arraybuffer"
}).success(function (data, status, headers, config) {
    console.log(data);
   var file = new Blob([data], { type: 'application/binary' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
var link=document.createElement('a');
link.href=fileURL;
link.download="testing.exe";
link.click();
    window.open(objectUrl);
}).error(function (data, status, headers, config) {
    //upload failed
});

and use Blob and pass type "application/binary" and create a link to download it.

Sign up to request clarification or add additional context in comments.

2 Comments

what if the file i huge? Will it be loaded into the browsers memory when we use blob?
Why not just get the response straight as a blob? e.g. responseType: 'blob' -> developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/…
3

Given code will help you to download exe file as well as to check browser compatibility.

var ieEDGE = navigator.userAgent.match(/Edge/g);
var ie = navigator.userAgent.match(/.NET/g); // IE 11+
var oldIE = navigator.userAgent.match(/MSIE/g); 

var blob = new window.Blob([data.data], { type: 'application/x-msdownload' });

if (ie || oldIE || ieEDGE) {

    var fileName="filename"+'.exe';
    window.navigator.msSaveBlob(blob, fileName);
}
else {

    var file = new Blob([ data.data ], {
        type : 'application/x-msdownload'
    });

    var fileURL = URL.createObjectURL(file);
    var a         = document.createElement('a');
    a.href        = fileURL; 
    a.target      = '_blank';
    a.download    = "filename"+'.exe';
    document.body.appendChild(a);

    a.click();

}
//Successfully Downloaded

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.