# Oracle Application Express File Download

I recently had a client complain that when clicking on a file from an ApEx application it always asked them to save or open the file rather than just rendering it in the browser via a plug-in (e.g. rendering a pdf in Acrobat Reader).  
  
I almost always want that, so it never seemed to be an issue to me, but I could see the point. I had utilized the standard ApEx routines for downloading files from a blob in my custom table. This has the following code:  
  
htp.p('Content-length: ' || v\_length);  
\-- the filename will be used by the browser if the users does a save as  
htp.p('Content-Disposition: attachment; filename="' || v\_file\_name || '"');  
\-- close the headers  
owa\_util.http\_header\_close;  
\-- download the BLOB  
wpg\_docload.download\_file( Lob\_loc );  
  
This code is fine and it does just what it will do, but the blue text causes (at least some, including IE) browsers to force a download, and never open within the browser.  
  
It was easy to comment out that line, but that left another issue. Now when the user does a save (right clicks and does "Save as" or saves from the plugin) it doesn't have the correct filename. It will have as a filename whatever comes after the last slash (/) in the url. That probably looks something like my\_package.download?p\_id=12442. Users really didn't like that.  
  
So . . . What to do? We changed the download links to look like this instead:  
/apexfiledownload//  
like this  
/apexfiledownload/12442/myFile.pdf  
  
Then we used an Apache rewrite rule to route that to our download routine (my\_package.download?p\_id=12442). Here is the rule:  
  
RewriteRule ^/apexfiledownload/(\[^/\]+)/?(.\*) /htmldb/my\_package.download?p\_id=$1 \[PT\]  
  
That works and everyone is happy now.  
  
I have an idea that I could also have implemented this by simply changing the offending line from attachment to inline:  
htp.p('Content-Disposition: inline; filename="' || v\_file\_name || '"');  
Testing that will be left to another day.
