Oracle Apex - Download File on Button Click Example

Oracle Apex - Download File on Button Click Example

Here I am giving an example of how to download a file on a button click in Oracle Apex. Follow these steps:

Download File on Button Click in Oracle Apex Example

Suppose you have a table where the BLOB data is stored, or you are just creating the BLOB data in PL/SQL, and you want to download that BLOB on a button click. To do this, in Oracle Apex, go to Shared Components > Application Process and create an Ajax Callback process and add the following PL/SQL code in it:

Declare
   b_blob blob;
   n_len number;
   v_mimetype varchar2(40) := 'application/pdf';
BEGIN
   Select yourBlobColumn into b_blob
      from yourBlobTable where yourColumn = 123;

   n_len := dbms_lob.getlength(b_blob);
   htp.flush;
   htp.init;
   -- you can change the mimetype according to the file type
   owa_util.mime_header(v_mimetype, false);
   htp.p('Content-length: '||n_len);
   htp.p('Content-Disposition: attachment; filename="abc.pdf"');
   htp.p('Set-Cookie: filedownload=true; path=/');

   owa_util.http_header_close;
 
   WPG_DOCLOAD.DOWNLOAD_FILE( b_blob );
END;

Then click on the Create button to finally Save the Ajax callback process. Suppose you have given the name download_my_file to this Ajax callback process.

Now for the button for which you want to download the file on click, follow the below steps:

On that button, create a dynamic action to execute JavaScript code. And add the following code:

javascript:window.open('f?p=&APP_ID.:0:&SESSION.:APPLICATION_PROCESS=download_my_file:NO', '_self');

In the above JavaScript command, you need to change the Ajax callback process name according to your process name.

This way, in Oracle Apex, you can download a file with a button click.

Ref: Question-OrclQA-9127