How to get a Blob Answer with Angularjs?

Asked

Viewed 279 times

1

I was reading in the MDN documentation that it is possible to get an answer 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 an answer Blob also through the $http.get angular.

I know by default, Angularjs is configured to send and receive application/json, but would like to know if there is somewhere to set up receiving Blob.

1 answer

1


I believe that the application/json be relative, anything can be blob, even if it is plain text, since Blob is a Javascript API:

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

The support details of responseType can be found in:

I don’t know much about Angularjs, but according to what I read in the documentation it would be something like:

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

From what I read the blob in XMLHttpRequest has a good support, the only place that will not work are browsers like IE9 or older.

In Angularjs it is possible to configure other things:

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

Yet this is another case.

  • You don’t need the 'config', actually. I can set { responseType: 'blob' } straightforward

  • @Wallacemaxters understood, did not imagine, I think I may have read wrong :) ... I will adjust.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.