To download a file from a server directory in Oracle APEX, you must typically bridge the gap between the server's file system and the web browser using a and PL/SQL . While APEX handles table-stored BLOBs natively, files on a physical disk require manual handling via BFILE pointers or the WPG_DOCLOAD package. Core Requirements
: The database user (schema) associated with your APEX workspace must have read access. GRANT READ ON DIRECTORY MY_FILE_DIR TO APEX_SCHEMA_NAME; Use code with caution. Method 1: Using a PL/SQL Page Process (Traditional) oracle apex download file from directory
CREATE OR REPLACE DIRECTORY MY_FILE_DIR AS '/u01/app/oracle/my_files'; Use code with caution. To download a file from a server directory
DECLARE l_bfile BFILE; l_filename VARCHAR2(255) := :P1_FILE_NAME; -- Item containing filename l_mime_type VARCHAR2(100) := 'application/octet-stream'; BEGIN -- Locate the file l_bfile := BFILENAME('MY_FILE_DIR', l_filename); -- Check if file exists IF DBMS_LOB.FILEEXISTS(l_bfile) = 1 THEN -- Close any existing open files to avoid errors SYS.HTP.INIT; -- Set HTTP Headers SYS.OWA_UTIL.MIME_HEADER(l_mime_type, FALSE); SYS.HTP.P('Content-Length: ' || DBMS_LOB.GETLENGTH(l_bfile)); SYS.HTP.P('Content-Disposition: attachment; filename="' || l_filename || '"'); SYS.OWA_UTIL.HTTP_HEADER_CLOSE; -- Stream the file SYS.WPG_DOCLOAD.DOWNLOAD_FILE(l_bfile); -- Stop further page processing APEX_APPLICATION.STOP_APEX_ENGINE; END IF; END; Use code with caution. Method 2: Modern Download Feature (APEX 24.1+) GRANT READ ON DIRECTORY MY_FILE_DIR TO APEX_SCHEMA_NAME; Use
: To use this with a server directory, you first convert the BFILE to a temporary BLOB within your query or a supporting function.