6

i need to set response Type as 'blob' in angular. how to set response Type for $http in angularjs? sample code for set responseType using XMLHttpRequest is provided below. i need to replace XMLHttpRequest with $http.

var oMyForm = new FormData();
oMyForm.append("js", content);
var oReq = new XMLHttpRequest({mozSystem: true});
oReq.open("POST", url, true);
oReq.responseType = "blob";
oReq.onload = function(xhr) {

};
oReq.onerror = function(xhr) {

};
oReq.send(oMyForm);
2
  • Why is your code not working? What error do you catch? Commented Sep 11, 2014 at 11:29
  • here i used XMLHttpRequest,need to replace with $http in angularjs Commented Sep 11, 2014 at 11:31

2 Answers 2

15

angular provides config object for that , check docs

  $http({
        url: "http://localhost:8080/example/teste",
        method: "POST",
        responseType: "blob"

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

Comments

0

This works in IE, Chrome and FF.

function download(url) {
    if (navigator.msSaveBlob) {
        var ajax = new XMLHttpRequest();
        ajax.open("GET", url, true);
        ajax.responseType = 'blob';
        ajax.onload = function(e) {
            navigator.msSaveBlob(data, url.substr(url.lastIndexOf("/") + 1));
        };
    } else {
        $('#download-frame').remove();
        $('<iframe/>', {
            src: url,
            id: 'download-frame'
        }).hide().appendTo('body');
    }
}

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.