1

So I've looking around a lot and try to see best way to open blob as pdf in browser. Closest stackoverflow question i was able to find was this one: XMLHttpRequest to open PDF in browser but this is 11 years old. Currently I have a program that looks like this:

var xhr = new XMLHttpRequest();
  xhr.open('GET', 'http://localhost:8080/api/download', true);
  xhr.responseType = 'blob';
  
  xhr.onload = function(e) {
    if (this.status == 200) {
      // Note: .response instead of .responseText
      var blob = new Blob([this.response], {type: 'application/pdf'}),
          url = URL.createObjectURL(blob);
          window.open(url, '_blank');     
    }
  };
  xhr.send();

Now again Issue here is that it opens in chrome browser ( FYI: Version 102.0.5005.61 (Official Build) (arm64)) with a UUID. So url of new tab looks something like this:

blob:http://localhost:3000/f7c5571a-bf59-45d1-99ea-e495fe859008

Now when I click on download icon. File explorer opens up with UUID in it and I cannot download it in windows. When I try it says network error.

Question: What is best possible way to display just pdf in browser. While removing downloading icons if they are not going to work.

Oh i have also tried creating the link and then doing link.download = fileName; That works perfectly fine but I am not looking to download file just view it on browser. I have also tried content-disposition inline and attachment. Does not work. Result is same.

Thanks a lot!

Ash
  • 261
  • 4
  • 16

1 Answers1

0

I was able to solve this by having service as a GET not POST. With GET call chrome browser behaves as it should. Chrome lools at content-disposition header and downloads or opens pdf in new file accordingly.

Ash
  • 261
  • 4
  • 16