【问题标题】:How to disable the office online viewer provided by Microsoft Edge when downloading office files using hyperlink?使用超链接下载office文件时如何禁用Microsoft Edge提供的office在线查看器?
【发布时间】:2022-09-28 13:59:47
【问题描述】:

最近遇到一个问题,我写了下面的HTML代码来实现文件下载:

<div id=\"downloadLinkListEl\">
    <a href=\"./xlsx/test0.xlsx?t=1663997904033\" target=\"_blank\">test0</a>
    <a href=\"./xlsx/test1.xlsx?t=1663997904033\" target=\"_blank\">test1</a>
    <a href=\"./xlsx/test2.xlsx?t=1663997904033\" target=\"_blank\">test2</a>
    <a href=\"./xlsx/test3.xlsx?t=1663997904033\" target=\"_blank\">test3</a>
</div>

以上所有文件的扩展名都是.xlsx,可以用Microsoft Excel打开。

在大多数浏览器中,代码都可以按照我们的预期运行——点击超链接后,会打开一个新窗口,然后启动下载任务。

但是,在 Edge (Chromium) 中,打开了两个窗口,第二个窗口将重定向到 Microsoft 提供的 Office Online Viewer——这是我们意想不到的。

其实这个可以通过修改Edge的默认设置来解决: \"Open Office files in the browser\" in setting

但是对于最终用户来说,用户体验很糟糕。

那么,在使用 Edge 时,有什么方法可以直接下载文件而不是重定向到 office 在线查看器?

  • 您可以尝试在&lt;a&gt; 标签中添加download attribute。然后我认为它会直接下载文件而不是在Edge中打开它。
  • @YuZhou 我试过了。但它不起作用。在线办公室查看器仍处于打开状态。

标签: html cross-browser microsoft-edge chromium web-frontend


【解决方案1】:

我认为这可能与文件请求的响应头有关,因此,我尝试对其进行修改。

我使用 nginx 来托管 excel 文件。

TL;博士

server {
  server_name example.net;
  listen 80;
  location / {
    root /var/www/poc;
    index  index.html index.htm index.php;
  }
  location /xlsx {
    root /var/www/poc;
    index  index.html index.htm index.php;

    # Here are two headers I've added.
    add_header Content-Disposition "attachment";
    add_header Content-Type "application/octet-stream";
  }
}

细节

最初,nginx 没有额外的配置。因此,当我们访问.xlsx 文件时,服务器将在Content-Type 标头字段中使用其默认文件类型进行响应。喜欢:

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

也许这是解决这个问题的关键? ?

所以我改变了它的默认Content-Type

add_header Content-Type "application/octet-stream";

在 nginx 配置中。

然后我发现这可能仍然无法正常工作- 我不确定这是否是由于响应中存在两个 Content-Type 标头引起的。

Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Type: application/octet-stream

但是在我添加另一个标题Content-Disposition 之后

add_header Content-Disposition "attachment";

此问题不再发生,文件可以直接下载。

问题可能得到解决?

有些事情还没有弄清楚

边界条件让 Edge 决定文件应该直接下载还是在在线查看器中打开。

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多