• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to get open or save dialog in struts.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Satish Kumar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks merrill. It worked.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 don't even know how to spell CIA. But this tiny ad does:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic