Solved

Force download of text file

Posted on 2002-05-01
11
442 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:TenTonJim
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 6984723
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
0
 
LVL 2

Author Comment

by:TenTonJim
ID: 6984776
Yes. I do not want it to render.

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

Thanks,
JimS.
0
 
LVL 2

Author Comment

by:TenTonJim
ID: 6984803
Yes. I do not want it to render.

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

Thanks,
JimS.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:coreyit
ID: 6985235
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
0
 

Expert Comment

by:menreeq
ID: 6986017
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
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6986125
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
0
 

Expert Comment

by:menreeq
ID: 6987037
Ohh i see, first time answering a question.  Thanks for the tip
0
 
LVL 2

Author Comment

by:TenTonJim
ID: 6987406
thank you menreeq, and I may use that answer.

cheekycj, could you explain a little further?

Thanks,
JimS.
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 6988664
menreeq: No Prob.. welcome to EE.

TenTonJim:  the trick is to trick the browser into believing you are sending it a file that it cannot display in the browser.

So what you do is:
import java.io.*; // for all the IO reading code.

then in the main code:

String filePath = this.getServletContext().getRealPath("/") + "/path/to/file/from/Servlet/Context";
File textFile = new File(filePath);
if (textFile.exists()) {
        ServletConfig config = getServletConfig();
        ServletContext application = config.getServletContext();
        response.setContentType ("application/octet-stream");
        response.setHeader ("Content-Disposition", "attachment;
filename=\"yourfilename.txt\"");
        response.setContentLength((int) textFile.length());
        // set output stream to the response's output stream
        ServletOutputStream servletoutputstream = response.getOutputStream();
        // now read file
        byte[] dataRead = new byte[(int)textFile.length()];
        FileInputStream fileinputstream = new FileInputStream(textFile);
        // read in byte data from file and store in byte array
        fileinputstream.read(dataRead, 0, (int)textFile.length());
        // close file
        if (fileinputstream != null) {
          fileinputstream.close();
        }
        // write the file to output stream
        servletoutputstream.write(dataRead);
        // flush the output stream
        servletoutputstream.flush();
      }
      else {
         // output something to indicate file does not exist
      }

I think that should be it.

CJ
0
 
LVL 2

Author Comment

by:TenTonJim
ID: 6988764
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.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 6994200
Glad I could help, Thanx for the "A"

CJ
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web service Rest Client creation 4 97
servlet concurrency 13 114
squirrelPlay java challenge 40 159
caughtSpeeding java challenge 16 140
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question