Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to manage responseType = 'blob' #479

Closed
montesq opened this issue Dec 16, 2013 · 19 comments
Closed

How to manage responseType = 'blob' #479

montesq opened this issue Dec 16, 2013 · 19 comments
Labels

Comments

@montesq
Copy link

montesq commented Dec 16, 2013

I call an API endpoint that returns a file. As you can see below, I success to implement a full javascript snippet to download the file using xhr :

    window.URL = window.URL || window.webkitURL;

    var xhr = new XMLHttpRequest();
    xhr.open('GET', Restangular.one('attachments', idAtt).getRestangularUrl(), true);
    xhr.setRequestHeader('X-Auth-Token', 'token');
    xhr.responseType = 'blob';

    xhr.onload = function(e) {
        if (this.status == 200) {
            var blob = this.response;
            var url= window.URL.createObjectURL(blob);
            window.open(url);
        }
    };
    xhr.send();

But now, I would like to use only Restangular instead! However, it doesn't seem to be possible setting the responseType to 'blob'...
I've tried to convert the text response to blob, but I finally get a corrupted file at the end.
Has someone already solved this kind of problem?

@mgonto
Copy link
Owner

mgonto commented Dec 16, 2013

Do you know how to do this with $http? If you do, I can tell you how to do it with Restangular from that :)

@mgonto
Copy link
Owner

mgonto commented Dec 16, 2013

It should actually work from Angular 1.2.5 and above :) angular/angular.js#1013

What angular version are you using?

@mgonto
Copy link
Owner

mgonto commented Dec 16, 2013

With 1.2.5, you should be able to do:

Restangular.one('attachments', idAtt).get({}, {"X-Auth-Token": 'token'}).then(function(response) {
  // Response is the blob :)
});

@mgonto
Copy link
Owner

mgonto commented Dec 16, 2013

Let me know if it does :)

@montesq
Copy link
Author

montesq commented Dec 16, 2013

Thank you for your prompt answer! Here is the $http solution :

    $http({method: 'GET', url: Restangular.one('attachments', idAtt).getRestangularUrl(),
        headers: {'X-Auth-Token': token}, responseType: 'blob'}).then(function(response) {
        var url = (window.URL || window.webkitURL).createObjectURL(response.data);
        window.open(url);
    });

But can't find in the restangular documentation where to define the responseType attribute ^^

@montesq
Copy link
Author

montesq commented Dec 16, 2013

In fact, the solution that you've suggested doesn't work because the response is not a blob (that is why it is necessary to put the additional attribute responseType: 'blob' with the $http solution)

@mgonto
Copy link
Owner

mgonto commented Dec 16, 2013

Then do:

Restangular.one('attachments', idAtt).withHttpConfig({responseType: 'blob'}}.get({}, {"X-Auth-Token": 'token'}).then(function(response) {
  var url = (window.URL || window.webkitURL).createObjectURL(response);
  window.open(url);
});

@mgonto
Copy link
Owner

mgonto commented Dec 17, 2013

Did that work?

@montesq
Copy link
Author

montesq commented Dec 17, 2013

Thank you for the answer! I'm currently at work, let me try this this evening (in 12hours) ;)

@montesq
Copy link
Author

montesq commented Dec 17, 2013

I guess you will not be surprised if I tell you that everything works fine ;)
Thank you again for your job!

@montesq montesq closed this as completed Dec 17, 2013
@mgonto
Copy link
Owner

mgonto commented Dec 17, 2013

That's awesome :).

withHttpConfig is used to set $http config that you want to be used for the following API call. It's useful for "not so common" cases like this one :).

@psmichalek
Copy link

How to set the appropriate MIME type? Right now showing as 'application/unknown' with some very strange file name.

@vishwajeetv
Copy link

In my case, response type is not blob , it's application/pdf . How can I appropriately manage to download the response (which is pdf file) ??

@devjon
Copy link

devjon commented Mar 28, 2015

@vishwajeetv : stuck on the same problem. Any heads up ??

@felixfbecker
Copy link

responseType is just what the type the response should be in your JS. Blob is for everything binary like a pdf or image. You can download it for example by constructing a link with URL.createObjectUrl() and then pointing an <a download=""> to it and clicking it, with window.open(), display it in iframe, etc etc

@DmitryGonchar
Copy link

In my case $window.open() resulted in "popup blocked" browser message, but
$window.location.href = url; worked perfectly (no message)

@luanntvn
Copy link

luanntvn commented May 19, 2016

blob would work but I use arraybuffer to be able to decoded error response to json if any

Restangular.one('downloadPDF')
            .withHttpConfig({ responseType: 'arraybuffer' })
            .get()
            .then(function (response) {
                var fileName = 'file.pdf';
                var a = document.createElement('a');
                document.body.appendChild(a);
                a.style = 'display: none';
                var file = new Blob([response], {type: 'application/pdf'});
                var fileURL = (window.URL || window.webkitURL).createObjectURL(file);
                a.href = fileURL;
                a.download = fileName;
                a.click();
                (window.URL || window.webkitURL).revokeObjectURL(file);
            }, function (response) {
                //error
            });

@doubl3p
Copy link

doubl3p commented Jun 21, 2017

you also can use the paket "angular-file-saver" and use it in the "then block":

Restangular.one('downloadExcel')
  .withHttpConfig({ responseType: 'arraybuffer' })
  .get().then(function(response) {
  
    var blob = new Blob([response], {
      type: "application/vnd.ms-excel ; charset=charset=utf-8"
    });
    saveAs(blob, "MyExcel.xls");

}, function (response){
  //error
});

type depends on your case: excel, pdf, etc.

@kennlebu
Copy link

Using withHttpConfig({responseType: 'blob'}) kept giving me an error The selected responseType is not supported

withHttpConfig({responseType: ResponseContentType.Blob}) worked for me instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants