|
Description:
|
If any struts action or servlet sends a known mime type (example text/xml or image/jpeg etc) to the response stream, the receiving browser will render it. But you might want to immediately prompt the user to save the response to a file directly to the user's disk, without opening it in the browser.
For this we need to set content-disposition header in the http response to override this default behavior of the browser.
For example, if response is xml and you want to raise a file dialog which will prompt user to save it to "output.xml", following http headers required:
ContentType=text/xml
Content-disposition: attachment; filename=output.xml
Content-disposition is an extension to the MIME protocol that instructs a MIME user agent on how it should display an attached file. The range of valid values for content-disposition are discussed in Request for Comment (RFC) 1806.
|
|
|
Example Code:
|
Following is a struts action which on invocation shows a file dialog on the browser:
|
|
About the Author
|
| Bharat Dighe is a Software Engineer with over 7 years of industry experience working in various Java projects. He can be reached at bdighe (at) yahoo (dot) com |
More examples by Bharat Dighe
|
|
|
|
public class FileDialogRaiseAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException, ErrorPageException
{
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=output.xml");
java.io.PrintWriter out = response.getWriter();
out.print("<output>);
out.print("test raise file dialog example output");
out.print("</output>);
return null;
}
}
|
|