【问题标题】:Not able to execute document.execCommand in Edge [duplicate]无法在 Edge 中执行 document.execCommand [重复]
【发布时间】:2020-04-07 08:50:46
【问题描述】:

我正在尝试将一些内容导出为 .csv 文件,如下面的代码所示。

function ExportToCSV(gridContent, file_name) {
    if (detectIE()) { // Checks IE and Edge
            var IEwindow = window.open();
            IEwindow.document.write(gridContent);
            IEwindow.document.close();
            IEwindow.document.execCommand('SaveAs', true, file_name + ".csv");
     } else {
          ..... for other browsers.........
     }
  }

我遇到问题 document.execCommand 无法像在 Internet Explorer 中那样运行,即提示对话框窗口存储 .csv 文件 internet explorer .

document.execCommand 在 Edge 中失败,我缺少什么?

【问题讨论】:

  • 你的意思是它没有显示保存文件对话框并直接下载文件吗?转到“设置”并检查“询问我如何处理每次下载”选项是否已禁用。让我们知道您在本次测试中使用的是哪个版本的 MS Edge 浏览器?
  • 1) Ask me what to do with each download 选项禁用 ==> 是的,它已禁用。 2) 您用于此测试的 MS Edge 浏览器版本? ==> 44.18362.449.0
  • 启用该选项。然后 Edge 将显示文件下载对话框。您是否收到任何错误或文件未下载?请尝试提供有关您的问题的详细信息。
  • @Shankar,这个问题有进展吗?如果您找到了解决方案,那么请尝试将其作为答案发布,并尝试在 48 小时后将您自己的答案标记为该问题的可接受答案,此时可以标记。如果问题仍然存在,请尝试参考线程上的建议,并让我们知道您对此问题的状态。我们将努力提供进一步的建议。感谢您的理解。
  • 我从这个答案中找到了解决方案 == > stackoverflow.com/questions/17836273/…

标签: javascript microsoft-edge


【解决方案1】:
function ExportToCSV(gridContent, file_name) {
if (detectIE()) { // Checks IE and Edge
     var _fileName = file_name + ".csv";
     var blob = new Blob([decodeURIComponent(encodeURI(gridContent))], {
         type: "text/csv;charset=utf-8;"
     });
     navigator.msSaveBlob(blob, _fileName);
 } else {
      ..... for other browsers.........
 }
}

【讨论】:

  • 感谢您发布此问题的解决方案。我建议您尝试在 48 小时后标记您自己对这个问题的答案,当它可以标记时。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
猜你喜欢
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 2012-09-02
相关资源
最近更新 更多