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

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:

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

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:....

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

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("Content-Disposition","filename=\""+ordObj.getSourceName()+"\"");
  3. register the AMISOrdPlayMediaServlet in web.xml
        <servlet>
    <servlet-name>ordDeliverMedia</servlet-name>
    <servlet-class>nl.amis.media.AMISOrdPlayMediaServlet</servlet-class>
    <init-param>
    <param-name>releaseMode</param-name>
    <param-value>Stateful</param-value>
    </init-param>
    </servlet>

And let’s run the application:

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

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

One Response

  1. Bernie October 20, 2007