Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Force download of text file

Posted on 2002-05-01
11
Medium Priority
?
448 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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