2

I have some files that I want people to be able to download or view in their browser as they wish. I cannot figure out how to do this simply, setting headers in the host or doing some javascript magic.

The end result should be an html page with 2 links per file listed, one to open in a new tab, one to download the file. I can get one, or the other but not both to work at the same time.

I tried both Content-Disposition options, and they both work but not concurrently. I tried the chrome.downloads api but it is not available in normal html script. I tried this but it download a text file with the link itself in it, rather than the file content. The download attribute is ignored in favour of the Content-Disposition headers, and default to inline, so even not setting it force open and does not allow download.

Content-Disposition: inline
Content-Disposition: attachment
<a href="test.txt" download="text.txt" target="_blank">

Can anyone spot what I am doing wrong?

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
teeel
  • 41
  • 1
  • 4
  • *Where* are you setting `Content-Disposition`? What does that code look like? I've never had a problem with a browser not handling it correctly when it's correctly sent from the server. – T.J. Crowder Aug 12 '19 at 17:16
  • My problem is that I need to be able to do both downloading and opening on the browser depending on what the user wants to do. 3 line csv? open in tab, 15000 line csv ? download. It's all about choices – teeel Aug 21 '19 at 12:14

2 Answers2

2

The Content-Disposition header needs to be sent by the server, and I believe in your case it can only appear once for a given file in the response (in your case, you're sending only one file, not multipart form data with multiple files). After all, it's a suggestion to the user agent about what it should do with that file. To implement your links, you'll need two separate URLs (if there's server-side scripting, the difference can be a query string): One that provides the Content-Disposition: inline response, and another that provides the Content-Disposition: attachment; filename="text.txt" response. Provided you do that, the links should work in any modern browser.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hum I'm using s3 as hosting so I don't have access to this, but this confirm my suspicion that there isn't any good way to do this with headers only. Thank you. – teeel Aug 21 '19 at 12:13
2

Sadly it doesn't seem like there is any ways to do that with headers only, I will need to get some amount of scripting involved.

teeel
  • 41
  • 1
  • 4