How to get a Blob response with AngularJs?

1

I was reading in the MDN documentation that it is possible to get a response Blob through ajax, according to the code below:

var oReq = new XMLHttpRequest();
oReq.open("GET", "/myfile.png", true);
oReq.responseType = "blob";

oReq.onload = function(oEvent) {
  var blob = oReq.response;
  // ...
};

oReq.send()

I liked the idea and would like to know if it is possible to get a response Blob also through $http.get of the Angular.

I know that by default, AngularJs is configured to send and receive application/json , but would like to know if it has any place to set up receipt of Blob .

    
asked by anonymous 09.08.2018 / 18:03

1 answer

1

I think that application/json is relative, anything can be blob, even being plain text, since Blob is a JavaScript API:

Used to represent raw data, as doc $http.get uses XmlHttpRequest , then it is possible to set the 'blob' value of responseType as shown in:

Support details for responseType can be found at:

I do not understand much of AngularJS, but what I read in the documentation would be something like:

$http.get('/someUrl', { 'responseType': 'blob' }).then(successCallback, errorCallback);

So I read blob in% with% has good support, the only place that will not work are browsers like IE9 or older.

In AngularJS you can configure other things:

  • XMLHttpRequest
  • method
  • url
  • params
  • data
  • headers
  • eventHandlers
  • uploadEventHandlers
  • xsrfHeaderName
  • xsrfCookieName
  • transformRequest
  • transformResponse
  • paramSerializer
  • cache
  • timeout
  • withCredentials

However this is already another case.

    
09.08.2018 / 18:12