wood burning stoves*
The moose likes Struts and the fly likes How to get open or save dialog in struts. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get open or save dialog in struts. " Watch "How to get open or save dialog in struts. " New topic
Author

How to get open or save dialog in struts.

Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
I am getting a pdf from the database and need to prompt the user to save or open the file in the browser. If possible please sent the sample code that would work on clicking the link or button that will prompt for the Open or save dialog. If possible also tell the way to get a pdf from the database and bring it to the action class for display.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm afraid that the open/save dialog box is not something that's in your direct control as a web application developer. Here's how it works:

When a file is requested through a URL on the browser, the browser tries to determine what type of file it is based on either the content-type in the header, or from the file's extension (.pdf, .xls, .html, .gif, etc.). If the file is of a type it knows how to display, the browser will simply display it. If it's not a file type the browser knows how to display, it will prompt the user whether to open the file or to save it.

In the case of a PDF file, when a user downloads the Adobe PDF reader, the browser plugin is also installed. That means the browser does know how to display a pdf file, and it will normally just display the file using the reader plugin without asking whether to save it or open it.

The best way around this is to educate your users that if they want to save the PDF file, to right-click the link and select "save Target as..." and that will cause the save dialog box to appear.

Another way would be to have your application "lie" about what type of file it is, indicating that it's some unknown file type. That will cause the open/save dialog box to be displayed.

Regarding how to download a file from a database, there are several links that will explain how to do that. Here are a couple of them:

http://javaalmanac.com/egs/java.sql/GetBlob.html
http://www.techmag.biz/struts_upload_download_files

You may also want to look into using the relatively new DownloadAction class.


Merrill
Consultant, Sima Solutions
Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
Hi Merill,
My action class's download method looks like

public ActionForward Download(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
//String name =(String)(request.getParameter("fileName"));
//Get the file from disk or database here
int fileSize = 500;
String fileName = "cofig.pdf
String path =
response.setContentLength( fileSize);
response.setContentType("application/cofig.pdf");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
response.setHeader("Cache-Control",
"max-age=" + TIMEOUT);
ServletOutputStream outStream = response.getOutputStream();

//write the file contents into output stream

outStream.flush();
return null;
}

I am facing two problems here...
I am not able to set a prespecified path and file for download from the hard disk and the second is that in the current scenario i am gettin the file as blank...is my setContentType right..? How do i give an input for the open save dialog dynamically without prompting the user for browsing. The file to be downloaded will be at a fixed location in the disk.
Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
Even after changing the action class as follows the result is same

public class UploadFileAction extends BaseAction {
static private final int TIMEOUT = 600;
private static final String CLASSNAME = "UploadFileAction";

/**
* getKeyMethodMap decides the method to be called based on the button
* selected by the user.
*
* @return Map Calls the corresponding method in the action class
*/
protected Map getKeyMethodMap() {
Map map = new HashMap();
map.put("method.Download", "Download");
return map;
}

public ActionForward Download(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
//String name =(String)(request.getParameter("fileName"));
//Get the file from disk or database here
/*int fileSize = 500;*/
UploadForm uploadForm = (UploadForm)form;
String fileName = uploadForm.getFile().toString();
int fileSize = uploadForm.getSize().length;
System.out.println("The file Size is + "+ fileSize);
System.out.println("***********************************");
response.setContentLength( fileSize);
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename="
+ fileName);
response.setHeader("Cache-Control",
"max-age=" + TIMEOUT);
ServletOutputStream outStream = response.getOutputStream();

//write the file contents into output stream

outStream.flush();
outStream.close();
return null;
}


}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The line:

//write the file contents into output stream

in the sample code is an instruction for you to write your own code to write the file contents into the output stream. There is no code to do this in the sample. You have to write it yourself. If you're getting the content from a BLOB in the database, look at the other link I gave you:

http://javaalmanac.com/egs/java.sql/GetBlob.html

Once you have the input stream from the BLOB and the output stream from the HttpServletResponse object, you simply create a loop to read from the input stream and write to the output stream. The link below shows an example of reading from an input stream and writing to an output stream.

http://javaalmanac.com/egs/java.io/CopyFile.html
[ November 25, 2006: Message edited by: Merrill Higginson ]
Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
Thanks merrill. It worked.
Arulanand Dayalan
Ranch Hand

Joined: Aug 10, 2005
Posts: 124
You can set the content disposition to Application, so that the browser Prompts a dialog and asks the user to Open or save the content.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, you are right, Arulanand. I now realize that what I said in my first post on this thread was not quite correct. What I said in my first post only applies in the case of a simple link to a file. If you provide a "content disposition" in the header, you can manipulate whether the open/save dialog box will appear.

In the sample code that I directed Sandeep to look at, the line:

Instructs the browser that this is an attachment, and that regardless of the file type, the open/save dialog box should appear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get open or save dialog in struts.
 
Similar Threads
Servlet and Excel problem
How to make sure the open/save dialog opens a new application instance ?
Select directory dialog
Downloading a binary file from DB
dialog box doesn't appear when downloading a PDF