question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to manage responseType = 'blob'

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
luanntvncommented, 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
            });

7reactions
mgontocommented, 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);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle error for response Type blob in HttpRequest
I am calling an http request using httpClient and using response Type as 'blob' but the problem is when it ...
Read more >
Response.blob() - Web APIs - MDN Web Docs
The blob() method of the Response interface takes a Response stream and reads it to completion. It returns a promise that resolves with...
Read more >
Handle Blobs requests with Axios the right way - Medium
The most tricky part here is to change the responseType . We don't want to get a response with Content-Type: application/json which is...
Read more >
response.blob - You.com | The Search Engine You Control
Note: If the Response has a Response.type of "opaque", the resulting Blob will have a Blob.size of 0 and a Blob.type of empty...
Read more >
HttpRequest - Angular
withCredentials: boolean, Read-Only. Whether this request should be sent with outgoing credentials (cookies). ; responseType: 'arraybuffer' | 'blob' | 'json' | ' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found