Javascript: Downloading txt file ...

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Hi,

I'm looking for a way of forcing a browser like IE to download a txt file instead of opening it.

I have a button in a html page, and I want the users to download a specific txt file. The problem is that if I change the url to the txt file, IE just opens it ... of course the users can then save the file, but I want to force them to download the file ... can it be done?

Code on the button (simplified):
MyForm.action = "some_related_path\\one_big_file.txt";
MyForm.target = 'Download_Window';
MyForm.submit();

HTML Form definition:
...
<form name="MyForm" action="other_page.jsp" method="post">
...


That opens a new IE window (if 'Download_Window' isn't already opened), and opens the txt file in it ... it's a really big txt file with a report, so I'm interested in forcing the download ...


Thanks.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
Don't know the exact mechanics in JSP, but in ASP you can specify that the content type of the output is unknown (x-unknown?), which generally makes the browser thing it should download the file...
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Both the above solutions are good (although, do any java servers actually use .htaccess?)

There is a solution build right into html, however. Add a type attribute to the the <a> tag. So: <a href="foo.txt" type="application/octet-stream" /> or whatever mime type you find suitable. application/octet-stream seems appropriate though.
 

b4u

Golden Member
Nov 8, 2002
1,380
2
81
Thanks for the answers so far.

But as I understand, you are talking about the anchor element tag, which may be related with a link ... the problem here, is that I'm trying to avoid a link ... and so I would need some javascript code to do that download ... any chance to do it?


Thanks again ;)