Link to home
Start Free TrialLog in
Avatar of TenTonJim
TenTonJim

asked on

Force download of text file

I need to know the syntax for...

when the user clicks on a hyperlink it downloads a .txt file instead of the browser rendering the .txt file.

Thank you,
JimS.
Avatar of cheekycj
cheekycj
Flag of United States of America image

usually it should render it with just a Hyperlink.

but you can write a JSP that will read the text file.

You can use FileReader to read the file.

and then set the content type of the JSP to
"text/html"

using response.setContentType("text/html");

CJ
Avatar of TenTonJim
TenTonJim

ASKER

Yes. I do not want it to render.

I want the download prompt box asking me where to save it.

Thanks,
JimS.
Yes. I do not want it to render.

I want the download prompt box asking me where to save it.

Thanks,
JimS.
This is a client-side setting.

A few options:
You could save the file as a different format, archive it in a zip file (silly for one text file), include text on you're webpage that suggest that your users chose the "save as" option.

-corey
I had the same question not to long ago and i found this code that worked for me...check out the link

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2183&lngWId=2
then you want to set the content type that would force the browser to prompt the user to save.. such as "application/octet-stream" or something.

menreeq:  please do not propose answers unless you are 200% sure it is the absolute only way to accomplish the task.  Please look at the bottom of this page for more info.

CJ
Ohh i see, first time answering a question.  Thanks for the tip
thank you menreeq, and I may use that answer.

cheekycj, could you explain a little further?

Thanks,
JimS.
ASKER CERTIFIED SOLUTION
Avatar of cheekycj
cheekycj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I got working...

response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment; filename=\""+ filename + "\"");
java.io.FileInputStream fileInputStream =new java.io.FileInputStream(filepath+filename);
int i;
while ((i=fileInputStream.read()) != -1) {
 out.write(i);
}
fileInputStream.close();
out.close();



Thanks everyone!

JimS.
Glad I could help, Thanx for the "A"

CJ