使用超链接下载办公文件时,如何禁用 Microsoft Edge 提供的办公联机查看器?

How to disable the office online viewer provided by Microsoft Edge when downloading office files using hyperlink?

提问人:gogoend 提问时间:9/19/2022 最后编辑:gogoend 更新时间:9/28/2022 访问量:918

问:

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

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

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

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

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

实际上,这可以通过修改 Edge 的默认设置来解决:设置中的“在浏览器中打开 Office 文件”

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

那么,在使用 Edge 时,有没有可能直接下载文件而不是重定向到 Office Online Viewer?

HTML 跨浏览器 Microsoft-Edge Chromium Web 前端

评论

0赞 Yu Zhou 9/20/2022
您可以尝试在标签中添加下载属性。然后我认为它会直接下载文件,而不是在 Edge 中打开它。<a>
0赞 gogoend 9/24/2022
@YuZhou我试过了。但它不起作用。联机办公室查看器仍处于打开状态。

答:

1赞 gogoend 9/24/2022 #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 没有额外的配置。因此,当我们访问该文件时,服务器将在标头字段中使用其默认文件类型进行响应。喜欢:.xlsxContent-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 决定文件应直接下载还是在在线查看器中打开的 BORDER CONDITION