Solved

Force download of text file

Posted on 2002-05-01
11
436 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
Moving applications to the cloud or switching services to cloud-based ones, is a stressful job.  Here's how you can make it easier.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now