Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

198
Visualizações
open pdf in browser using javascript xmlhttprequest

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!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda