Download File on Button Click Example in Oracle APEX

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.

Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted