You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by de...@struts.apache.org on 2004/11/26 10:05:51 UTC

[Apache Struts Wiki] Updated: StrutsFileDownload

   Date: 2004-11-26T01:05:50
   Editor: NiallPemberton <ni...@apache.org>
   Wiki: Apache Struts Wiki
   Page: StrutsFileDownload
   URL: http://wiki.apache.org/struts/StrutsFileDownload

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -10,6 +10,7 @@
     * ResourceStreamInfo Example 
     * Bye Array Example 
  * Using the DownloadAction in you Web Pages
+ * Content Disposition
 
 
 (niallp: Since I haven't actually used this class myself, I'm hoping that Martin, Frank or anyone else involved in the discussion/creation of this would take a look here to see if its OK)
@@ -192,3 +193,56 @@
 
 }}}
 
+== Content Disposition ==
+
+=== Setting the Content Disposition ===
+DownloadAction doesn't cater for setting the content dispositon header. I guess at the moment the best way to do that would be override the execute() method and set it first, something like this...
+
+{{{
+
+public class ExampleFileDownload extends DownloadAction{
+
+
+
+    protected StreamInfo getStreamInfo(ActionMapping mapping, 
+                                       ActionForm form,
+                                       HttpServletRequest request, 
+                                       HttpServletResponse response)
+            throws Exception {
+        
+        // Download a "pdf" file - gets the file name from the
+        // Action Mapping's parameter
+        String contentType = "application/pdf";
+        File file          = new File(mapping.getParameter());
+
+        return new FileStreamInfo(contentType, file);
+        
+    }
+
+    public ActionForward execute(ActionMapping mapping,
+                                 ActionForm form,
+                                 HttpServletRequest request,
+                                 HttpServletResponse response)
+             throws Exception {
+
+        response.setHeader("Content-disposition", 
+                           "attachment; filename=" + mapping.getParameter());
+
+        return super.execute(mapping, form, request, response);
+
+    }
+}
+
+}}}
+
+Probably would want to play with the file name, to remove any path info first.
+
+=== Content Disposition Values ===
+
+You can set the content disposition to either download the file or open up in the browser:
+ * To open up in the browser: "inline; filename=myFile.pdf"
+ * To download: "attachment; filename=myFile.pdf"
+
+Displaying images I always set the content disposition to the "inline" option.
+
+'''NOTE:''' I'm not expert at this, just got it working by trial and error. I had problems and I'm seem to remember I had to play with setting the response headers to get it to work properly. There may also be browser issues - my app only has to work with IE. Anyone who knows more about this, feel free to amend this page.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org