Have Download File functionality in JHeadstart display the proper file name in the "Save File As" dialog

1

Creating ‘download file’ functionality in our ADF Web Applications is pretty easy, especially when using JHeadstart. Using a few declarative settings, we can generate the download capability in no time at all:

There is one just little thing left to be desired: the download dialog for some reason does not know the correct name of the file we are downloading:....

Here we are downloading AdfMatrixDemo.zip but the browser calls it ordDeliverMedia.zip, a generic name used for all files we download.

Fortunately, this can be remedied very easily. (this happens to have been my very first job with Java technology, back in 1999). All we have to do is set a header in the Response object in which we return the file:

response.setHeader("Content-Disposition","filename=\""+theFileName+"\""); 

This informs the browser – or at least the ones that are listening, as some browsers I believe ignore this piece of information – of the correct file name. 

You may – and probably should -wonder where you can inject this line of code – and where to get the filename from. It turns out to be pretty simple for JHeadstart environments.

All downloads are handled by the OrdPlayMediaServlet that is configured in the web.xml file. If we register our own extension of this Servlet, and set the required response header in it, we are in business. The steps:

  1. create class AMISOrdPlayMediaServlet, extending oracle.jheadstart.ord.html.OrdPlayMediaServlet; let’s keep it simple and just copy all code from OrdPlayMediaServlet to AMISOrdPlayMediaServlet.
  2. locate the renderContent() method; add a single line of code, following the assignment of ordObj:
    res.setHeader(&quot;Content-Disposition&quot;,&quot;filename=\&quot;&quot;+ordObj.getSourceName()+&quot;\&quot;&quot;);<br />
  3. register the AMISOrdPlayMediaServlet in web.xml
        &lt;servlet&gt;<br />        &lt;servlet-name&gt;ordDeliverMedia&lt;/servlet-name&gt;<br />        &lt;servlet-class&gt;nl.amis.media.AMISOrdPlayMediaServlet&lt;/servlet-class&gt;<br />        &lt;init-param&gt;<br />            &lt;param-name&gt;releaseMode&lt;/param-name&gt;<br />            &lt;param-value&gt;Stateful&lt;/param-value&gt;<br />        &lt;/init-param&gt;<br />    &lt;/servlet&gt;<br /><br />

And let’s run the application:

And there we have it: the browser uses the correct filename!

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Hi,

    I have an application in JDeveloper (10.1.3) with an af:golink whcih has the same behaviour you show above. Can you tell me how/where I can ‘inject the line of code’ so that the filename will appear instead of OrdDeliverMedia, as you describe above (I have the filename on the form, but not as part of the link – The form displays an image and the filename of the image is a label).

    Thanks.