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

187
Visualizações
Right way to allow a user to download a file when its URL is generated

Situation:

  • File is located on AWS S3
  • User presses button to download a file
  • Request is sent to the server
  • Server in response sends generated URL that grants access to the file
    • The URL of course links to another domain (to the S3 bucket)

Current solution:

  • Right now JS handler changes the window's location to the received URL.
  • File is downloaded as it should.
  • But browsers throws errors like:
    • Chrome: “Resource interpreted as Document but transferred with MIME type application/octet-stream”
    • Safari: “Failed to load resource: Frame load interrupted”

I cannot replace the button with the simple <a href="url" download> because the URL is generated in response to user's request and it is not known in advance.

I want it to be a one-click experience (i.e., I don't want the user to click a new button with the received URL).


Question:

  • What is the right way to allow a user to download a file when its URL is generated?
  • Is it OK to change the window.location for that purpose?
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

It not sound right to change window.location for download file..

If you using a <a> tag you can add the download attribute

 <a href="my.pdf" download="your.pdf" >download file</a>

If you prefer using code, you can use script like this:

 var aTag = document.createElement('a');
 aTag.setAttribute('download','your.pdf');
 aTag.setAttribute('href','my.pdf');
 aTag.click();
over 4 years ago · Santiago Trujillo Relatório

0

Right way is make backend send proper Content-Type header with correct MIME type of file on link download request.

It is ok to change window.location or document.location.href for page redirect to download link.

over 4 years ago · Santiago Trujillo Relatório

0

This is actually a very simple problem that needs no Javascript at all.

When the request is sent to the server by simply clicking a link to make the request, the server -- after validating the request, generates a signed URL, then responds...

HTTP/1.1 302 Found
Location: https://example-bucket.s3.amazonaws.com/... # new signed URL

The browser follows the redirect and fetches the resource.

over 4 years ago · Santiago Trujillo 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