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?
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?
Share Improve this question edited Aug 13, 2019 at 11:35 Julian Reschke 42.1k8 gold badges102 silver badges99 bronze badges asked Aug 12, 2019 at 17:12 teeelteeel 591 gold badge1 silver badge4 bronze badges 2-
1
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 Commented Aug 12, 2019 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 Commented Aug 21, 2019 at 12:14
2 Answers
Reset to default 3The 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.
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.