Now that we have covered everything related to document management in our previous articles, the next step is to implement the file download functionality, allowing users to retrieve and view the files stored in our repository. Additionally, it is important to note that for files of type “assistant and messages”, we need to block the download functionality, as these files cannot be downloaded.

The reason for this restriction is that “assistant and messages” files typically contain dynamically generated content or sensitive information tied to conversational context, which is not meant to be directly accessed or retrieved in raw form. Allowing downloads of such files could lead to data mismanagement, potential breaches of confidentiality, or misuse of dynamically generated content that is only relevant within its intended application environment.

We will implement some changes in how we create the link, and this time, we will do it from the SQL query section of the interactive report to customize the messages displayed to the user.

Copy to Clipboard

The onclick event in DELETE_LINK triggers the custom event 'deleteFile' in APEX using:

apex.event.trigger(document, 'deleteFile', ID);

For this to work, the Custom Event Name in the APEX Dynamic Action must match exactly ('deleteFile'), and the event should be set to trigger on document to capture the action globally.

If the event name differs in APEX (e.g., typos, extra spaces, or different casing), the delete action won’t execute. Keeping this consistency ensures that APEX correctly processes the file deletion based on the passed ID.

Oracle APEX Page Designer interface showing a dynamic action setup for downloading files, with a custom event 'downloadFile' highlighted in red.

An Oracle APEX Page Designer view illustrating the configuration of the ‘DA_DOWNLOAD_FILE’ dynamic action, with the custom event ‘downloadFile’ highlighted for triggering file download processes

As seen in the image, the Dynamic Action DA_DOWNLOAD_FILE follows a structured process with four key steps to handle file downloads in Oracle APEX efficiently:

  1. SET FILE ID: Assigns the selected file’s ID to a page item (:P20010_FILE_ID).

Oracle APEX Page Designer interface displaying the 'SET FILE ID' dynamic action configuration with JavaScript expression 'this.data' to set the file ID dynamically.

Oracle APEX Page Designer view highlighting the configuration of the ‘SET FILE ID’ action, which uses a JavaScript expression to dynamically set the file ID

  1. PR_GET_FILE_NAME: Calls a REST API to fetch the filename associated with the file ID and stores it in :P20010_FILE_NAME.

Copy to Clipboard

It is important to remember that in “Items to Submit”, we must reference the page item that needs to be sent to the PL/SQL process—in this case, P20010_FILE_ID, which is required to fetch the file name.

Oracle APEX Page Designer interface showing the 'PR_GET_FILE_NAME' process, which uses PL/SQL code to retrieve a file name via a REST API call, with specific items to submit and return highlighted

Oracle APEX Page Designer view featuring the ‘PR_GET_FILE_NAME’ process, configured to fetch a file name using PL/SQL and REST API integration, with P20010_FILE_ID and P20010_FILE_NAME set as the items to submit and return

Similarly, in “Items to Return”, we must specify where the retrieved data will be stored—in this case, P20010_FILE_NAME, which will hold the filename extracted from the REST API response.

  1. PR_DOWNLOAD_FILE: Retrieves the file’s content using the OpenAI API, stores it in an APEX collection (FILE_DOWNLOAD_COLLECTION) along with the filename.

Copy to Clipboard

For the download process, we only need to submit P20010_FILE_ID and P20010_FILE_NAME, as these are the essential fields used to retrieve and store the file.

Oracle APEX Page Designer interface showcasing the 'PR_DOWNLOAD_FILE' process, which uses PL/SQL code to handle REST API calls for file downloads, with defined constants and items to submit and return.

Oracle APEX Page Designer configuration of the ‘PR_DOWNLOAD_FILE’ process, designed to retrieve file content from a REST API and prepare it for user download using server-side PL/SQL logic

  1. DOWNLOAD: A new APEX 24.1 feature that simplifies file downloads. It reads the file from the collection and serves it to the user with minimal configuration, requiring only a SQL query with columns for the file content (BLOB), filename, and MIME type.

Copy to Clipboard
Oracle APEX Page Designer interface displaying the 'DOWNLOAD' action configuration, which retrieves file content from an APEX collection using an SQL query, with settings for file download as an attachment

Oracle APEX Page Designer setup for the ‘DOWNLOAD’ action, using an SQL query to fetch file content, name, and MIME type from an APEX collection for seamless file download

This streamlined approach ensures efficient file retrieval and downloading, leveraging APEX collections for temporary storage and REST API integration for file access.

For more information, please visit our company website or find us on APEX World for additional resources and updates!