Wednesday, September 20, 2006

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.

2 comments:

Anonymous said...

inline works a treat

Anonymous said...

Using "inline" does NOT give you the results you're looking for: it still uses the procedure name as the file name when the user does a "Save As". I'm still searching for a solution. For now I'm using "attachment" which forces the Download File dialog to always display but provides the custom file name that is required. I'd like to find a solution that opens the file automatically with the proper file name.