【问题标题】:Download File From Server on Firefox and IE在 Firefox 和 IE 上从服务器下载文件
【发布时间】:2018-05-25 15:35:25
【问题描述】:

我已使用此 Gist(不是我的)从服务器下载 CSV 文件。 它在 Chrome、Edge 和 Safari 中运行良好。

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

在 IE 中打开新标签页,在 FireFox 中提示下载,但没有文件扩展名。

特别是,我希望能够向 Firefox 添加扩展。这不可能吗?

【问题讨论】:

    标签: javascript firefox browser download


    【解决方案1】:

    删除对 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;
      }
    
    }

    【讨论】:

    • 嘿!这很有效,至少对 Firefox 来说是这样!它也将名称更改为正确的文件名。 IE11 仍会打开一个新标签页,但我认为这是我们可以接受的。
    • 很高兴听到 :) 很高兴我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多