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:
- create class AMISOrdPlayMediaServlet, extending oracle.jheadstart.ord.html.OrdPlayMediaServlet; let’s keep it simple and just copy all code from OrdPlayMediaServlet to AMISOrdPlayMediaServlet.
- locate the renderContent() method; add a single line of code, following the assignment of ordObj:
res.setHeader("Content-Disposition","filename=\""+ordObj.getSourceName()+"\"");
- 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:
And there we have it: the browser uses the correct filename!
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.