1

I am trying to download multiple files at once. At first I use window.location = url but now it doesn't seem to work.

I've changed it to window.open(url, "_blank") and it only downloads the first one:

window.open("/host/Controller/DownloadDasFiles?paramId=204");
window.open("/host/Controller/DownloadDasFiles?paramId=205");
window.open("/host/Controller/DownloadDasFiles?paramId=206");
public FileResult DownloadDasFiles(int paramId)
{
  var dasControl = UnityConfig.container.Resolve<IDasControlService>();
  var filename = dasControl.GetDasFileToDownload(paramId);
  return File(filename, "application/octet-stream", Path.GetFileName(filename));
}

In my real case I do this after AJAX success in a javascript loop, but this code should work, shouldn't it?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Mikelon85
  • 432
  • 1
  • 13
  • 30
  • 6
    The problem is because the browser will stop you spamming the user by opening multiple tabs/windows in quick succession. If you need to do this, I'd suggest a better solution would be to zip up the files on the server and start a single download to that zip file. – Rory McCrossan Aug 29 '18 at 07:47
  • 1
    Better to return a ZIP file containing all downloaded files rather than executing `window.open` multiple times: https://stackoverflow.com/questions/42671760/download-multiple-files-selected-with-mvc5 & https://stackoverflow.com/questions/12713710/returning-multiple-files-from-mvc-action. – Tetsuya Yamamoto Aug 29 '18 at 07:48
  • Thanks, returning a ZIP is what I'm developing now. I suspected something like that, thanks for the confirmation – Mikelon85 Aug 29 '18 at 08:02
  • Possible duplicate of [window.open not not able to open more than two links](https://stackoverflow.com/questions/13467915/window-open-not-not-able-to-open-more-than-two-links) – TAHA SULTAN TEMURI Aug 29 '18 at 09:43

1 Answers1

0

This may help. It will open all 3 windows at same time.

let test = [204, 205, 206];
let downLoadFileBaseUrl = '/host/Controller/DownloadDasFiles?paramId='
test.forEach(element => {
  let file = `${downLoadFileBaseUrl}+${element}`;
  window.open(file, '_blank', 'File :' + element + ',scrollbars=1,menubar=0,resizable=1,width=850,height=500');
});
Backs
  • 24,430
  • 5
  • 58
  • 85