• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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.
0
TenTonJim
Asked:
TenTonJim
  • 4
  • 4
  • 2
  • +1
1 Solution
 
cheekycjCommented:
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
 
TenTonJimAuthor Commented:
Yes. I do not want it to render.

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

Thanks,
JimS.
0
 
TenTonJimAuthor Commented:
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!

 
coreyitCommented:
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
 
menreeqCommented:
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
 
cheekycjCommented:
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
 
menreeqCommented:
Ohh i see, first time answering a question.  Thanks for the tip
0
 
TenTonJimAuthor Commented:
thank you menreeq, and I may use that answer.

cheekycj, could you explain a little further?

Thanks,
JimS.
0
 
cheekycjCommented:
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
 
TenTonJimAuthor Commented:
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
 
cheekycjCommented:
Glad I could help, Thanx for the "A"

CJ
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now