8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

APEX Tips : File Download From a Button or Link

This article describes how to create a button or link to initiate a file download from an APEX application.

Related articles.

PL/SQL Code

There is a separate article describing some variations of how to code the PL/SQL side of the file download process.

For this example, we are going to assume you have a table called DOCUMENTS that holds your media in a BLOB column, along with a column for the MIME type. Here is the code we would use to initiate the file download.

CREATE OR REPLACE PROCEDURE get_file (p_file_name  IN VARCHAR2) IS
  l_bfile BFILE;
BEGIN
  l_blob_content  documents.blob_content%TYPE;
  l_mime_type     documents.mime_type%TYPE;
BEGIN
  SELECT blob_content,
         mime_type
  INTO   l_blob_content,
         l_mime_type
  FROM   documents
  WHERE  file_name = p_file_name;

  sys.HTP.init;
  sys.OWA_UTIL.mime_header(l_mime_type, FALSE);
  sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(l_bfile));
  sys.HTP.p('Content-Disposition: filename="' || p_file_name || '"');
  sys.OWA_UTIL.http_header_close;

  sys.WPG_DOCLOAD.download_file(l_bfile);
  apex_application.stop_apex_engine;
EXCEPTION
  WHEN OTHERS THEN
    HTP.p('Whoops');
END;
/

APEX Common Setup (Application Item and Process)

The following APEX setup is required, whether you plan to initiate the download from a link or a button.

Create a new Application Item.

Create a new Application Process.

APEX Button

The following process associates the button with the GET_FILE application process, passing in a FILE_ID value of "my_image.png".

This button hard codes the image to download, but the image name could be sourced from an item on the screen.

APEX Link

The following HTML defines a link that calls the GET_FILE application process, passing in a FILE_ID value of "my_image.png".

<a href="f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=GET_FILE:::FILE_ID:my_image.png">Download my_image.png</a>

This link could be hard-coded in a Static Content region, or generated dynamically.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.