Solved

Force download of text file

Posted on 2002-05-01
11
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
In this blog we highlight approaches to managed security as a service.  We also look into ConnectWise’s value in aiding MSPs’ security management and indicate why critical alerting is a necessary integration.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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