Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Bloqueo de la ejecución hasta que el usuario seleccione una opción del modo de descarga del navegador

Estoy usando window.location.href = some_url en un bit de javascript. Esto abre un menú del navegador, así:

ingrese la descripción de la imagen aquí

Esto ya está sucediendo dentro de una promesa: tengo otro .then() después, pero me gustaría que esperaran hasta que el usuario haya seleccionado algo de ese menú.

¿Alguna forma de acceder a esto? Bits de código:

 $.ajax({ url: url, type: "POST", data: formData, contentType: false, processData: false }) .done(function(response, jqXHR){ new Promise(function(resolve, reject){ if (response.download){ console.log("downloading files sent.... ") window.location.href=response.download.url // I'd like to block here on the popup window before moving resolve(response); } else { console.log("No downloads.") resolve(response); } return response }) .then(...)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

De acuerdo, entonces la respuesta real es que no debes ser perezoso y usar algo como un blob en su lugar. Del lado del cliente, algo así:

 $.ajax({ url: url, type: "POST", data: formData, contentType: false, processData: false }) .done(function(response, jqXHR){ console.log("POST success,") if (response.download.url) { console.log("content to download") fetch(response.download.url) .then(function (resp){ let blob = resp.blob() const header = resp.headers.get('Content-Disposition'); let filename = header.split(';')[1].split('=')[1]; response["filename"] = filename return blob // return resp }) .then( function(blob) { // blob = resp.blob() const blob_url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.style.display = "none" a.href = blob_url a.download = response.filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(blob_url) // alert("download should be finished now") return response }) .then(function (response){ if (response.current_page && response.current_page.refresh) { console.log("Refreshing page...") location.reload(true); console.log("Done.") } else { console.log("No refresh.") } return response }) .catch((err)=>{console.log(err)} )

Luego, del lado del servidor: estoy usando django, pero los detalles serían similares en otros marcos:

 logger.info(f"Creating tmp file for pdf response") with tempfile.NamedTemporaryFile() as file: file.write(pdf) file.seek(0) stream = file.read() # build the http response httpresponse = HttpResponse(content_type='application/pdf;', content=stream) httpresponse['Content-Disposition'] = f'attachment; filename={value["filename"]}' httpresponse['Content-Transfer-Encoding'] = 'binary' return httpresponse

Esto funciona, porque luego estoy esperando que termine la llamada fetch() antes de pasar a construir el blob() y recuperarlo localmente. En otras palabras, la "espera" es administrada por la promesa fetch() y solo continúa una vez que el servidor devolvió el archivo.

Entonces puedo actualizar la página sin preocuparme, ya que tengo los datos en el caché del navegador en ese momento. Perder el estado con una recarga () en ese punto está bien.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!