Скачать файл с сервера в Firefox и IE - PullRequest
0 голосов
/ 25 мая 2018

Я использовал этот Gist (не мой) для загрузки файла CSV с сервера.Он отлично работает в Chrome, Edge и Safari.

https://gist.github.com/handleman/6ac609c084c735e6738f5d155e29a874

В IE открывается новая вкладка, а в FireFox запрашивается загрузка, но без расширения файла.

В частности, я хотел бы иметь возможность добавить расширение для Firefox.Разве это не возможно?

1 Ответ

0 голосов
/ 25 мая 2018

Снимите проверки для IE и Firefox.

Обновленная версия:

function downloadFile(sUrl) {
  //iOS devices do not support downloading. We have to inform user about this.
  if (/(iP)/g.test(navigator.userAgent)) {
    // alert('Your device does not support files downloading. Please try again in desktop browser.');
    window.open(sUrl, '_blank');
    return false;
  }

  //Creating new link node.
  var link = document.createElement('a');
  link.href = sUrl;
  link.setAttribute('target','_blank');

  if (link.download !== undefined) {
    //Set HTML5 download attribute. This will prevent file from opening if supported.
    var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
    link.download = fileName;
  }

  //Dispatching click event.
  if (document.createEvent) {
    var e = document.createEvent('MouseEvents');
    e.initEvent('click', true, true);
    link.dispatchEvent(e);
    return true;
  }

}
...