Skip to Main Content

APEX

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PL/SQL to download a binary file via attaching to a webpage?

Craig StevensonMar 30 2022

I am working in PL/SQL. I am trying to open a binary file, in my example a
PDF, and proceed to attach it to a webpage so it can be downloaded.

In code:
-- declare:
file_cur utl_file.file_type;
raw_data raw(32000);
buff_size PLS_INTEGER := 32000;

-- setup mime:
owa_util.mime_header('application/octet-stream', FALSE);
htp.p('Content-Disposition: attachment; filename=download.pdf');

-- get my file:
file_cur := UTL_FILE.FOPEN ('DATALOAD', 'test.pdf', 'r', 32000);
UTL_FILE.GET_RAW (file_cur, raw_data, buff_size);

-- write my file:
htp.prn(raw_data); -- this totally fails to do what I want, it converts to hex

I also try:
htp.prn(HEXtoNCHR(raw_data)); -- which fails for extended ascii characters

where HEXtoNCHR is:
-- ***********************************************************************
-- *** Function HEXtoNCHR(hexstring) ** RETURN converted string
-- ***********************************************************************
FUNCTION HEXtoNCHR(hex_item IN varchar2) RETURN varchar2 AS

hexchars varchar2(16) := '0123456789ABCDEF';
v_string varchar2(32000) := NULL;

ptr number;
char1 integer;
char2 integer;

BEGIN
ptr := 1;
WHILE ptr < length(hex_item) LOOP
char1 := instr(hexchars, substr(hex_item, ptr, 1)) -1;
ptr := ptr + 1;
char2 := instr(hexchars, substr(hex_item, ptr, 1)) -1;
ptr := ptr + 1;
v_string := v_string || NCHR(char1*16+char2);
END LOOP;
RETURN v_string;
END;

This fails for extended ascii codes that are above or equal to 128 in decimal
notation.

What is the right way to download something binary from a webpage? Is there an
alternative to htp.prn for this situation? Or, is there an opportunity to use
something other than a raw datatype?

Comments
Post Details
Added on Mar 30 2022
1 comment
895 views