Solved

How to initiate the download dialog box?

Posted on 2008-10-16
8
1,163 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:killdurst
8 Comments
 
LVL 5

Accepted Solution

by:
Devario Johnson earned 250 total points
Comment Utility
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
 
LVL 1

Author Comment

by:killdurst
Comment Utility
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
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
When you are serving the text file, add "Content-Disposition: attachment" header
0
 
LVL 1

Author Comment

by:killdurst
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
> response.sendRedirect(response.encodeRedirectURL(requestURL));

You should not redirect. You should server file from here (read from file and write to servlet)
0
 
LVL 1

Author Comment

by:killdurst
Comment Utility
I sorta knew that that line was wrong, but I don't know what I should write. What should I write?
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
Comment Utility
You could use something like the following.
http://www.exampledepot.com/egs/javax.servlet/GetImage.html
0
 
LVL 44

Expert Comment

by:scrathcyboy
Comment Utility
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This video teaches viewers about errors in exception handling.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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

11 Experts available now in Live!

Get 1:1 Help Now