8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23ai | Misc | PL/SQL | SQL | RAC | WebLogic | Linux
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_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_blob_content)); sys.HTP.p('Content-Disposition: filename="' || p_file_name || '"'); sys.OWA_UTIL.http_header_close; sys.WPG_DOCLOAD.download_file(l_blob_content); apex_application.stop_apex_engine; EXCEPTION WHEN apex_application.e_stop_apex_engine THEN NULL; 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.
- Shared Components > Application Items
- Click the "Create" button.
- Enter the following details.
- Name: FILE_ID
- Scope: Application
- Session State Protection: Checksum Required - User Level
- Click the "Create Application Item" button.
Create a new Application Process.
- Shared Components > Application Processes
- Click the "Create" button.
- Enter the following details.
- Name: GET_FILE
- Sequence: {accept the default}
- Process Point: Ajax Callback: Run this application process when requested by a page process.
- Click the "Next" button.
- Enter the PL/SQL to perform the download.
BEGIN get_file(:FILE_ID); END;
- Click the "Next" button.
- Select the "User is Authenticated (not public)" condition type.
- Click the "Create Process" button.
- If you need to add authoriszation, click on the new application process, select the authorization scheme and click the "Apply Changes" button.
APEX Button
The following process associates the button with the GET_FILE
application process, passing in a FILE_ID
value of "my_image.png".
- Highlight the button you want to use to initiate the download.
- In the property panel, under the "Behavior" section, select the "Action" of "Redirect to URL".
- Click the "No Link Defined" button next to the "Target".
- Enter the following URL.
f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=GET_FILE:::FILE_ID:my_image.png
- Click the "OK" button.
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...