【问题标题】:Download attribute on A tag not working in IEA 标签上的下载属性在 IE 中不起作用
【发布时间】:2013-08-26 00:50:50
【问题描述】:

从下面的代码中,我创建了一个下载文件的动态锚标记。此代码在 Chrome 中运行良好,但在 IE 中运行良好。我怎样才能让它工作

<div id="divContainer">
    <h3>Sample title</h3>
</div>
<button onclick="clicker()">Click me</button>

<script type="text/javascript">

    function clicker() {
        var anchorTag = document.createElement('a');
        anchorTag.href = "http://cdn1.dailymirror.lk/media/images/finance.jpg";
        anchorTag.download = "download";
        anchorTag.click();


        var element = document.getElementById('divContainer');
        element.appendChild(anchorTag);
    }

</script>

【问题讨论】:

  • 第一次点击时为什么还要麻烦appendChild呢?广告为什么在可以执行 location=URL 时点击它
  • 我认为创建新的 a 标签没有意​​义。为什么单击“单击我”按钮时不触发下载。网络用户不喜欢点击

标签: html internet-explorer anchor


【解决方案1】:

Internet Explorer 目前不支持 A 标签上的 Download 属性。

见http://caniuse.com/download和http://status.modern.ie/adownloadattribute;后者表示该功能在 IE12 中处于“考虑中”。

【讨论】:

  • 所以没有办法在IE中设置文件名?
  • 您的问题不够精确。文件传递服务器选择文件名。
  • 我在 IE 中找到了解决方法,而不是创建 &lt;a&gt; 标签。只需使用这一行...navigator.msSaveBlob(blob, fileName); 归功于 download.js 以下行:github.com/rndme/download/blob/master/download.js#L143
  • 2017 仍然没有 IE12...不过,Edge 支持此功能。
  • 这没有回答“我怎样才能让它工作”的问题
【解决方案2】:

就我而言,由于需要支持使用 IE 11(版本 11.0.9600.18665),我最终使用了@Henners 在他的评论中提供的解决方案:

// IE10+ : (has Blob, but not a[download] or URL)
if (navigator.msSaveBlob) {
    return navigator.msSaveBlob(blob, fileName);
}

非常简单实用。

显然,这个解决方案是在 dandavis 创建的 Javascript download 函数中找到的。

【讨论】:

  • 我试过这个并且得到 blob 是未定义的。你能解释一下如何定义 blob 以及在其中放入什么。我只有文件的 URL。有没有办法从 URL 将文件加载到 blob 中?
  • 或许this answer可以帮你实现你想要的转化。之后,您只需要按照我的回答中提到的那样触发下载。
【解决方案3】:

老问题,但我想我会添加我们的解决方案。这是我在上一个项目中使用的代码。它并不完美,但它通过了所有浏览器和 IE9+ 的 QA。

downloadCSV(data,fileName){
  var blob = new Blob([data], {type:  "text/plain;charset=utf-8;"});
  var anchor = angular.element('<a/>');

  if (window.navigator.msSaveBlob) { // IE
    window.navigator.msSaveOrOpenBlob(blob, fileName)
  } else if (navigator.userAgent.search("Firefox") !== -1) { // Firefox
    anchor.css({display: 'none'});
    angular.element(document.body).append(anchor);

    anchor.attr({
      href: 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(data),
      target: '_blank',
      download: fileName
    })[0].click();

    anchor.remove();
  } else { // Chrome
    anchor.attr({
      href: URL.createObjectURL(blob),
      target: '_blank',
      download: fileName
    })[0].click();
  }
}

在 IE 中使用 ms 特定的 API 最适合我们。另请注意,某些浏览器要求锚点实际上位于 DOM 中才能使下载属性起作用,而 Chrome 则不需要。此外,我们发现 Blob 在各种浏览器中的工作方式存在一些不一致之处。一些浏览器也有导出限制。这允许在每个浏览器 afaik 中最大可能的 CSV 导出。

【讨论】:

  • target="blank" 在 IE11 上对我来说很好用,为什么不是完整的答案?
  • @Kevin 你能解释一下数据格式应该是什么吗?
  • OP 没有提到 Angular,这个答案依赖于它。
【解决方案4】:

自 build 10547+ 起,Microsoft Edge 浏览器现在支持 a 标签上的 download 属性。

&lt;a href="download/image.png" download="file_name.png"&gt;Download Image&lt;/a&gt;

边缘功能更新:https://dev.windows.com/en-us/microsoft-edge/platform/changelog/desktop/10547/

[下载]标准:http://www.w3.org/html/wg/drafts/html/master/links.html#attr-hyperlink-download

【讨论】:

    【解决方案5】:

    此代码片段允许在 IE、Edge 和其他现代浏览器中将 blob 保存在文件中。

    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
    
        if (request.readyState === 4 && request.status === 200) {
    
            // Extract filename form response using regex
            var filename = "";
            var disposition = request.getResponseHeader('Content-Disposition');
            if (disposition && disposition.indexOf('attachment') !== -1) {
                var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                var matches = filenameRegex.exec(disposition);
                if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
            }
    
            if (window.navigator.msSaveOrOpenBlob) { // for IE and Edge
                window.navigator.msSaveBlob(request.response, filename);
            } else {
                // for modern browsers
                var a = document.createElement('a');
                a.href = window.URL.createObjectURL(request.response);
                a.download = filename;
                a.style.display = 'none';
                document.body.appendChild(a);
                a.click();
            }
        }
    
        button.disabled = false;
        dragArea.removeAttribute('spinner-visible');
        // spinner.style.display = "none";
    
    };
    request.open("POST", "download");
    request.responseType = 'blob';
    request.send(formData);
    

    对于 IE 和 Edge 使用:msSaveBlob

    【讨论】:

      【解决方案6】:

      使用我的功能

      它将你的atag绑定到IE中的下载文件

      function MS_bindDownload(el) {
          if(el === undefined){
              throw Error('I need element parameter.');
          }
          if(el.href === ''){
              throw Error('The element has no href value.');
          }
          var filename = el.getAttribute('download');
          if (filename === null || filename === ''){
              var tmp = el.href.split('/');
              filename = tmp[tmp.length-1];
          }
          el.addEventListener('click', function (evt) {
              evt.preventDefault();
              var xhr = new XMLHttpRequest();
              xhr.onloadstart = function () {
                  xhr.responseType = 'blob';
              };
              xhr.onload = function () {
                  navigator.msSaveOrOpenBlob(xhr.response, filename);
              };
              xhr.open("GET", el.href, true);
              xhr.send();
          })
      }
      

      【讨论】:

        【解决方案7】:

        先添加子元素,然后点击

        或者你可以使用 window.location= 'url' ;

        【讨论】:

        • 如果我先附加孩子然后点击。它重定向到图像而不是下载
        【解决方案8】:

        正如前面的回答中提到的,IE 不支持下载属性。作为一种解决方法,您可以使用 iFrame 下载文件。这是一个示例代码 sn-p。

        function downloadFile(url){
            var oIframe = window.document.createElement('iframe');
            var $body = jQuery(document.body);
            var $oIframe = jQuery(oIframe).attr({
                src: url,
                style: 'display:none'
            });
            $body.append($oIframe);
        
        }
        

        【讨论】:

        • 这应该如何工作?我在结束 &lt;/body&gt; 标记之前附加了一个 iframe,其中包含我的文件源。
        • 如果 header 中的 content-disposoition 是 attachment ,它会尝试下载而不是在 iFrame 中渲染。
        • header('Content-Disposition: attachment; filename="some filename"');
        【解决方案9】:

        我从here 复制了代码,并针对 ES6 和 ESLint 进行了更新,并将其添加到我的项目中。

        您可以将code 保存到download.js 并在您的项目中使用它,如下所示:

        import Download from './download'
        Download('/somefile.png', 'somefile.png')
        

        请注意,它支持 dataURLs(来自画布对象)等等...有关详细信息,请参阅 https://github.com/rndme。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-20
          • 1970-01-01
          • 2018-12-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多