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

Viewed 34

Recently I encounter a issue that I've written the following HTML code to implement file download:

<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>

The extension name of all of the above files are .xlsx, which can be opend by Microsoft Excel.

In most of the browsers, the code can be run as what we expected - after the hyperlink being clicked, a new window will be opened, then a download task will be started.

However, in Microsoft Edge (Chromium), there were two window opened, and the second window will redirect to office online viewer provide by Microsoft - this is what we unexpected.

In fact, this can be resolved by modifying the default setting of Microsoft Edge: "Open Office files in the browser" in setting

but the user experience is terrible for the end users.

So is there any possible way to download the file directly rather than redirect to office online viewer provide by Microsoft when using Edge?

1 Answers

I think this may relevant to the response header of the file request, thus, I try to modify the it.

I use nginx to host the excel file.

TL;DR

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";
  }
}

Detail

Initally, there is no extra config for nginx. Thus, when we access the .xlsx file, the server will response with its default file type in Content-Type header field. Like:

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

Maybe it's the key to solve this issue?

So I changed its default Content-Type with

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

in nginx config.

Then I find this may still not working - I'm not sure if this is caused by there are two Content-Type header both exist in response.

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

But after I add the another header Content-Disposition with

add_header Content-Disposition "attachment";

this issue is not happend again, and the file can be directly download.

The issue may likely be resolved?

Something still not being clarified

The BORDER CONDITION to let the Edge decide the file should be downloaded directly or opened in online viewer.

Related