DBA Hub

📋Steps in this guide1/4

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.

oracle miscconfigurationintermediate
by OracleDba
24 views
1

PL/SQL Code

There is a separate article describing some variations of how to code the PL/SQL side of the file download process. - Download Files Using PL/SQL and a Gateway (ORDS, mod_plsql, EPG) For this example, we are going to assume you have a table called that holds your media in a column, along with a column for the MIME type. Here is the code we would use to initiate the file download.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
/
2

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 - 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. - 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. - 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.

Code/Command (click line numbers to comment):

1
2
3
BEGIN
  get_file(:FILE_ID);
END;
3

APEX Button

The following process associates the button with the application process, passing in a 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. - 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.

Code/Command (click line numbers to comment):

1
f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=GET_FILE:::FILE_ID:my_image.png
4

APEX Link

The following HTML defines a link that calls the application process, passing in a value of "my_image.png". This link could be hard-coded in a Static Content region, or generated dynamically. For more information see: - Download Files Using PL/SQL and a Gateway (ORDS, mod_plsql, EPG) - APEX Articles Hope this helps. Regards Tim...

Code/Command (click line numbers to comment):

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

Comments (0)

Please to add comments

No comments yet. Be the first to comment!