Pages

Sunday, September 6, 2009

Struts - DownloadAction

Every struts starter is facing the problem while writing the code for uploading a file. Especially to reduce the burden of writing long code for uploading struts introduced DownloadAction as an added feature from 1.2.6 version onwards. Using this action very easy like eating banana.

i am giving small example on how to use DownloadAction

=======================================================================

public class downloadFileAction extends org.apache.struts.actions.DownloadAction {

protected StreamInfo getStreamInfo(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// File Name
String fileName = "c:\\WriteSelectedRows.pdf";

/*
* To open up in the browser: "inline; filename=myFile.pdf"
* To download: "attachment; filename=myFile.pdf"
* URL for reference: http://wiki.apache.org/struts/StrutsFileDownload
*/

// Set the content disposition
response.setHeader("Content-disposition",
"attachment; filename=" + fileName);

// Download a "pdf" file - gets the file name from the
// Action Mapping's parameter
String contentType = "application/pdf";
File file = new File(fileName);

return new FileStreamInfo(contentType, file);

}
}

=======================================================================

if you want to know more about the Download Action Please go through the URL specified in the above example.

No comments:

Post a Comment