How to initiate the download dialog box?

On my JSP page I have a link that says "Download as text". When I click it, it will go to a servlet where a txt file will be created. The servlet would then redirect to this txt file. The problem is, when I click on the "Download as text" link, the file will be displayed on the browser itself. How do I change this so that when the link is clicked, the download dialog box will be displayed instead?
LVL 1
killdurstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Devario JohnsonSoftware EngineerCommented:
usually browsers will download a .zip file by default.   This is the way that you will be sure it will actually download it using the "save as" dialog box.

How do I force the browser to download a file? How do I force the browser to show/play a file itself? How do I force a file to be opened by a particular program?

You can't do any of these things reliably, because the Web doesn't work that way.

When the browser requests a document (hypertext, image, audio, multimedia, etc.), the server tells the browser what type of file it is. The server should be configured to identify a document's media type properly.

The browser then decides what to do with it. Different browsers are able to and are configured to display different types of documents themselves. Browsers are usually configured to handle other file types by using appropriate helper applications or by offering to save the document to the filesystem.

RFC 2183 describes the Content-disposition header, which can be used to suggest how the browser should handle documents. The value "inline" suggests that the document should be displayed automatically, and the value "attachment" suggests that the user should be prompted before the document is displayed (or saved). Note that this mechanism has significant security implications, which are described in the RFC.

Most browsers allow users to download to disk if they want to. If the file must be saved to disk, if there is absolutely no other way to handle it, then the MIME type could be "application/octet-stream". However, this essentially says, "I can't or won't tell you what this is." Furthermore, the MIME type "application/octet-stream" provokes incorrect behavior in MSIE's HTTP implementation. A better alternative would be a custom MIME type like "application/x-some-explanation", which avoids these problems.

Content Type                 Description
application/msword       Microsoft Word Document
application/octet-stream       Unclassified binary data (often used for compressed file or executable)
application/pdf               PDF Document
application/vnd.ms-excel       Microsoft Excel spreadsheet
application/zip              ZIP archive
audio/x-wav                      WAV audio format
audio/midi                      MIDI audio format
audio/x-pn-realaudio       RealAudio
image/gif       GIF                  image format
image/jpeg                     JPEG image format
image/png                     PNG image format
text/css                             CSS style sheet
text/html                             HTML document
text/plain                        Plain text
video/mpeg                     MPEG video format
video/quicktime       QuickTime video format
video/x-msvideo       AVI video format

Another method of ensuring that your file is properly sent to the client is to compress it into a standard compression format. Virtually all servers are set to handle the .zip extension and it is widely recognized by users.

Some servers (NCSA, Apache, and others) can be configured to support user-configured content types. Details are server dependent, so consult your server admin or documentation.

Note that Internet Explorer incorrectly ignores server-provided MIME types, so it sometimes "does the right thing" when the server is misconfigured. Other browsers correctly heed the server-provided MIME types, so they will reveal server misconfigurations.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
killdurstAuthor Commented:
I'm also looking into some of the HttpServletResponse methods. Below are what I came up with, but it's still not working. Are there any errors in the code?

String requestURL = "test.txt";
response.setContentType("text/html");
response.setHeader("Content-Disposition", "attachment;filename=test.txt");
response.sendRedirect(response.encodeRedirectURL(requestURL));

"response" is a HttpServletResponse object.
0
Ajay-SinghCommented:
When you are serving the text file, add "Content-Disposition: attachment" header
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

killdurstAuthor Commented:
Hi Ajay, I think we posted at around the same time. Is my post above yours what you are talking about? I've already added the header.
0
Ajay-SinghCommented:
> response.sendRedirect(response.encodeRedirectURL(requestURL));

You should not redirect. You should server file from here (read from file and write to servlet)
0
killdurstAuthor Commented:
I sorta knew that that line was wrong, but I don't know what I should write. What should I write?
0
rrzCommented:
You could use something like the following.
http://www.exampledepot.com/egs/javax.servlet/GetImage.html
0
scrathcyboyCommented:
The choice of whether to download or view a file is completely controlled by the user's browser settings.  Essentially ALL browsers will simply VIEW a TXT file.  Even if it is a TEXT file, give it a ZIP extension, or zip it, and then almost all browsers will initiate the download save-as dialog box.  The first reply said this TOO.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.