Downloads in Oracle APEX with APEX_HTTP.DOWNLOAD

Tiempo de lectura: 2 minutos

πŸ‘‹ Introduction

Struggling to deliver files from your Oracle APEX app?
Dealing with messy headers, complex conversions, or unsupported file types?

Oracle APEX 24.2 introduced APEX_HTTP.DOWNLOAD, a clean and efficient way to download files from your app using PL/SQL. Whether you’re generating dynamic reports, exporting PDFs, or delivering binary documentsβ€”this procedure handles it all.

In this post, you’ll learn:

  • What APEX_HTTP.DOWNLOAD does and when to use it
  • How to implement it in a real APEX app
  • How to handle CLOB to BLOB conversion
  • Best practices for secure, fast, and user-friendly downloads

πŸ“š Table of Contents

  1. What is APEX_HTTP.DOWNLOAD?
  2. Syntax and Parameters
  3. Real Example: PDF Contract Download
  4. Special Cases: MIME Types & CLOB Handling
  5. Best Practices
  6. Conclusion and Course Recommendation

1. What is APEX_HTTP.DOWNLOAD?

APEX_HTTP.DOWNLOAD is a built-in procedure in Oracle APEX that allows you to stream files (usually stored as BLOBs) directly to the user’s browser.

Unlike older approaches (HTP.P, OWA_UTIL, or ORDS hacks), this method is native, clean, and secure.

It works well for downloadable reports, dynamic files, user uploads, and even runtime-generated content.


2. Syntax and Parameters

APEX_HTTP.DOWNLOAD(
p_content_disposition IN VARCHAR2,
p_mime_type IN VARCHAR2,
p_content IN BLOB,
p_filename IN VARCHAR2 DEFAULT NULL );
  • p_content_disposition: 'inline' or 'attachment'
  • p_mime_type: MIME type of the file (e.g., 'application/pdf')
  • p_content: File content as a BLOB
  • p_filename: Optional file name to show in the download prompt

3. Real Example: PDF Contract Download

Assume you have a table CONTRACTS that stores PDF documents:

CREATE TABLE contracts (
id NUMBER PRIMARY KEY,
customer VARCHAR2(100),
pdf_file BLOB,
file_name VARCHAR2(255),
mime_type VARCHAR2(100)
);

You create a page with a “Download” button and a PL/SQL process:

DECLARE
l_blob BLOB;
l_mime VARCHAR2(100);
l_name VARCHAR2(255);
BEGIN
SELECT pdf_file, mime_type, file_name
INTO l_blob, l_mime, l_name
FROM contracts
WHERE id = :P1_CONTRACT_ID;

APEX_HTTP.DOWNLOAD(
p_content_disposition => 'attachment',
p_mime_type => l_mime,
p_content => l_blob,
p_filename => l_name
);
END;

βœ… The user gets a properly named file, downloaded directly in their browser.


4. Special Cases: MIME Types & CLOB Handling

If you’re dealing with CLOB data (e.g., text files or HTML), convert it to BLOB first:

FUNCTION clob_to_blob(p_clob IN CLOB) RETURN BLOB IS
l_blob BLOB;
BEGIN
DBMS_LOB.CREATETEMPORARY(l_blob, TRUE);
DBMS_LOB.CONVERTTOBLOB(l_blob, p_clob, DBMS_LOB.LOBMAXSIZE, 1, 1);
RETURN l_blob;
END;

Common MIME types:

  • PDF β†’ application/pdf
  • Excel β†’ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • Image β†’ image/png or image/jpeg
  • CSV β†’ text/csv

Use 'inline' for files that can be viewed in the browser, and 'attachment' for downloads.


5. Best Practices

βœ… Always validate the file exists before calling the procedure.
βœ… Use correct MIME types to avoid browser issues.
βœ… Set appropriate file names to improve UX.
βœ… Secure access to the procedure using session checks or user roles.
βœ… Log downloads if the file is sensitive or audited.


6. Conclusion

APEX_HTTP.DOWNLOAD is the go-to method for file delivery in modern APEX applications. It’s elegant, efficient, and supports both simple and advanced scenarios.

No more fiddling with HTTP headers or ORDS workarounds.


πŸŽ“ Want to Learn More?

Check out my full course on Oracle APEX file handling:

πŸ‘‰ Access the full APEX course at Aprendiz Academy
Includes lessons on BLOBs, CLOBs, PDF generation, ORDS integration, and secure download patterns.

Post a Comment

Your email address will not be published. Required fields are marked *