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

237
Visualizações
How to force PDF download in Firefox?

I have following code for downloading PDF files from backend:

    const link = document.createElement('a')

    link.href = URL.createObjectURL(blob)

    link.setAttribute('target', '_blank')
    link.setAttribute('download', file_name)

    link.click()
    link.remove()
    setTimeout(() => window.URL.revokeObjectURL(url), 100)

It works in Chrome as expected, and I believe it worked in Firefox before, but now Firefox opens file in the same tab instead of downloading, ignoring target and download attributes.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This is expected behaviour for 98 and above. PDFs are set to open in Firefox by default.

If you change the mimetype you give the Blob to "application/octet-stream" or target the link whose href you assign to an iframe within your page, I think you'll get the previous behaviour back.

When the download attribute applies, it is treated the same way as a "Content-Disposition: attachment" header sent by the server. The Firefox developers think the user's preferences should win over what the server says - irrespective of whether the user has indicated they want the PDFs to open in an external app (Acrobat or w/e), automatically save to disk, or opening in Firefox.

Source: https://bugzilla.mozilla.org/show_bug.cgi?id=1756980

Edit:
Firefox opening the file in the same tab and ignoring the target attribute, is a bug.

Source: https://bugzilla.mozilla.org/show_bug.cgi?id=1759916

about 4 years ago · Juan Pablo Isaza Relatório

0

This is an example using fetch for an existing blob. You might want to skip first URL.createObjectURL and construct new Blob straight from the file.

Using this approach downloads pdf in the Firefox, but opens it in a new tab PDF viewer anyway.

const blobUrl = URL.createObjectURL(blob);
const filename = 'attachment.pdf';

fetch(blobUrl).then(res => res.arrayBuffer()).then(res => {
  const file = new Blob([res], {type: 'application/octet-stream'});
  const fileURL = URL.createObjectURL(file);
  const link = document.createElement('a');
  link.href = fileURL;
  link.download = filename;
  link.click();
  link.remove();
});

about 4 years ago · Juan Pablo Isaza Relatório
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