While working on a customized application sometimes we need to create the application documentation or user manual for the end users. We can upload this documentation in the backend table and share a link to download.
This article describes how to create a link to initiate a file download from an APEX application.
Step 1: PL/SQL Code
For this example, I am 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 I would use to initiate the file download.
CREATE OR REPLACE PROCEDURE get_file (p_doc_id 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 document_id = p_doc_id;
sys.HTP.init;
sys.OWA_UTIL.mime_header(l_mime_type, FALSE);
sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(l_blob_content));
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;
/
Here, document_id is the primary key column from documents table.
Step 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
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.
Step 3: APEX URL
Create a new entry in Desktop Navigation Bar List.
Shared Components > Lists > Desktop Navigation Bar
Click the "Create Entry" button.
Enter the following details.
Image/Class: fa-download
List Entry Label: Download
Target Type: URL
URL Target:
f?p=&APP_ID.:1:&APP_SESSION.:APPLICATION_PROCESS=get_file:::FILE_ID:2391
Here, we are providing the document id as hard code value.
In this example, I have used a pdf document. When you click on the download link it will open the pdf document.
I hope this example will be helpful.
Thanks for reading !!
Kommentare