Forum Moderators: open

Message Too Old, No Replies

Text file link that downloads, not displays?

         

toonerama

1:59 pm on May 24, 2008 (gmt 0)

10+ Year Member



Hello,

Is it possible to have link to a text file (.txt) that will download when clicked, not display in a browser window?

Thanks.

Staffa

2:53 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, but you could zip your file (*.zip) and that will download.

penders

5:58 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it possible to have link to a text file (.txt) that will download when clicked...

I think yes, but you'll need to do something server-side (PHP or .htaccess) to send a different content-type header (as opposed to text/plain). Providing the browser obeys the header then it should force a download, but no guarantee.

You could have a look at the end of this thread.
[webmasterworld.com...]

(Search for 'force download' - more often used with media files and PDFs)

toonerama

11:08 pm on May 24, 2008 (gmt 0)

10+ Year Member



Thanks. So far I can't make the .htaccess idea work, but I will be investigating.

penders

11:06 am on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To be honest I've not tried this with .txt files, but it's the same idea in theory. As always, try this in different browsers, as browser behaviour could certainly vary in this respect. Check IE's setting... Internet Options > Security > "Open files based on content, not file extension" - Default: Enable.

The server will probably already be sending a content type header of 'text/plain' - so it is this that you need to override.

I would also clear your browser cache. If the file is already in your cache (presumably you have already linked to it) then it won't request it from the server; it won't check the .htaccess file.

Do you have permission to use .htaccess files on your server?

---<edit>---
The .htaccess method mentioned above works OK for me (to force a download of a standard .txt file in a directory). Tested on Windows IE6/7, FF1.5/2, Op8 (and Op9 see below) and Safari3. The browser prompts with a dialog box to 'Open' or 'Save' the file (except Safari). Selecting 'Open' downloads the file and opens it in Notepad (by default on my Windows machine). Without the .htaccess file the .txt file is simply opened in the browser window.

Safari doesn't prompt, it simply downloads the .txt file (to wherever you've specified in preferences) and opens it in Notepad (not as secure in my opinion).

Opera, however, opens the file in the browser window if you select 'Open'.

Opera9 was slightly odd, in that the very first time I linked to the .txt file in the browser it opened it without prompting (in the browser window), subsequent times it prompted!?