?
Solved

How to initiate the download dialog box?

Posted on 2008-10-16
8
Medium Priority
?
1,174 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
[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
8 Comments
 
LVL 5

Accepted Solution

by:
Devario Johnson earned 1000 total points
ID: 22737598
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
ID: 22737716
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
ID: 22737734
When you are serving the text file, add "Content-Disposition: attachment" header
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 1

Author Comment

by:killdurst
ID: 22737750
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
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 22737816
> 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
ID: 22738055
I sorta knew that that line was wrong, but I don't know what I should write. What should I write?
0
 
LVL 28

Assisted Solution

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

Expert Comment

by:scrathcyboy
ID: 22745962
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

770 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