1

I am trying to download a file and then reload the page.

The issue is that this code works only when the file download is completed within a second.
If the file is any bigger the first request to download the file seems to be blocked by the second request.

javascript:

//downloads the file
function updateAndDownload() {

  var link = document.createElement('a');
  link.href = "updateAndDownloadFile.rails?id=$ID";
  document.body.appendChild(link);
  link.click();
  PageReload();
}
//wait for a second and refresh the page
function PageReload() {
  setTimeout(function() {
    refreshPage();
  }, 1000);
}

function refreshPage() {
  // Close dialog.
  Control.Modal.close();

  // Reload.
  window.location.href = "index.rails?id=" + ID
}

I expect the file to be downloaded and the page to be refreshed maybe back to back or at the same time

Kaiido
  • 123,334
  • 13
  • 219
  • 285
Tausif
  • 123
  • 1
  • 3
  • 10
  • I did modify your sentence with what I thought I understood. However, I am not 100% sure of my own understanding, so if I misinterpreted your message, feel free to [edit] again your question. – Kaiido Apr 05 '19 at 05:33
  • I'm sure there are dupes of this... Can't find thme right now though... There is [this one](https://stackoverflow.com/questions/27991634/completion-status-of-html5-download), with no answer... – Kaiido Apr 05 '19 at 05:39

2 Answers2

0

instead of this..

// Reload. "window.location.href = "index.rails?id=" + ID"

use this... it works on my program //Reload "window.location.reload(true);"

Cj Pascual
  • 26
  • 2
0

Can you try with adding target attribute to link.

function updateAndDownload() {

  var link = document.createElement('a');
  link.href = "updateAndDownloadFile.rails?id=$ID";
  
  /** Add this line **//
  link.target = '_blank';
  
  document.body.appendChild(link);
  link.click();
  PageReload();
}
//wait for a second and refresh the page
function PageReload() {
  setTimeout(function() {
    refreshPage();
  }, 1000);
}

function refreshPage() {
  // Close dialog.
  Control.Modal.close();

  // Reload.
  window.location.href = "index.rails?id=" + ID
}
Anil Talla
  • 709
  • 5
  • 19