Skip to content

Display images

This document covers sample implementation for template GET handler displaying uploaded image using a custom upload REST template handlers. Both examples displays uploaded images into the plug-in sample application table UC_FROALA_SAMPLE_BLOBS.

:::note handler features

The handler can be combined with handlers uploading an image Simple, Default Parameters and Custom parameters

:::

:::note handler features

  • must be combined with handler uploading an image Image checksum
  • uses access token static function decode to decode an image checksum
  • if checksum is missing or malformed returns HTTP status 403 Forbidden

:::

Handler displaying uploaded image must meet the following requirements:

  1. must use GET method
  2. must be compilable at the run time
  3. must return 200 HTTP status
  4. must stream uploaded image content into browser buffer
AttributeValue
RESTful Service ModuleA custom RESTservice
Module Base Path/customrest/
URI Templatedisplay-simple/:fileid
Full URLhttps://example-domain.com/ords/workspacename/customrest/display-simple/:fileid
Handler MethodGET
Source TypePL/SQL
Mime Types AllowedNot defined
AttributeValue
RESTful Service ModuleA custom RESTservice
Module Base Path/customrest/
URI Templatedisplay-checksum/:filename
Full URLhttps://example-domain.com/ords/workspacename/customrest/display-checksum/:filename
Handler MethodGET
Source TypePL/SQL
Mime Types AllowedNot defined

The upload handler doesn’t require registering any resource parameters as long as REST specific bind variable :status_code is used to set request HTTP status.

The handler displaying uploaded image can be defined as PL/SQL code or media resource.

declare
v_param_fileid varchar2(4000) := :fileid;
v_file_row UC_FROALA_SAMPLE_BLOBS%ROWTYPE;
begin
select
*
into
v_file_row
from
UC_FROALA_SAMPLE_BLOBS
where
id = to_numbeR(v_param_fileid)
;
sys.HTP.init;
sys.OWA_UTIL.mime_header(v_file_row.FILE_MIMETYPE, FALSE);
sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(v_file_row.FILE_CONTENT));
sys.HTP.p('Access-Control-Allow-Origin: *');
sys.HTP.p('Content-Disposition: filename="' || v_file_row.FILE_NAME || '"');
sys.OWA_UTIL.http_header_close;
sys.WPG_DOCLOAD.download_file(v_file_row.FILE_CONTENT);
:status_code := 200;
exception
when no_data_found then
:status_code := 404;
when others then
:status_code := 400;
end;
declare
v_param_filename varchar2(4000) := :filename;
v_param_checksum varchar2(4000) := :checksum;
v_checksum_decoded varchar2(4000);
v_image_id number;
v_file_row UC_FROALA_SAMPLE_BLOBS%ROWTYPE;
begin
/* if checksum query parameters is missing force HTTP status 403 */
//highlight-start
if v_param_checksum is null then
raise_application_error(-20001, 'Checksum not included');
end if;
//highlight-end
/* checksum fails decododing when it's malformed */
//highlight-start
begin
v_checksum_decoded := UC_RTE_ACCESS_TOKEN.decode( v_param_checksum );
exception
when others then
raise_application_error(-20001, 'Failed decoding checksum');
end;
//highlight-end
/* checksum was created using filename and file id separated with coma */
/* for example "pngaaa.com-4093606.png,29662" */
//highlight-next-line
v_image_id := apex_string.split(v_checksum_decoded, ',')(2);
select
*
into
v_file_row
from
UC_FROALA_SAMPLE_BLOBS
where
id = v_image_id
;
sys.HTP.init;
sys.OWA_UTIL.mime_header(v_file_row.FILE_MIMETYPE, FALSE);
sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(v_file_row.FILE_CONTENT));
sys.HTP.p('Access-Control-Allow-Origin: *');
sys.HTP.p('Content-Disposition: filename="' || v_file_row.FILE_NAME || '"');
sys.OWA_UTIL.http_header_close;
sys.WPG_DOCLOAD.download_file(v_file_row.FILE_CONTENT);
:status_code := 200;
exception
when no_data_found then
:status_code := 404;
when others then
if SQLCODE = -20001 then
//highlight-next-line
:status_code := 403;
else
:status_code := 500;
end if;
end;

The handler output must be an image content streamed into the browser buffer. It can be done using handler source type media resource or PL/SQL code.

declare
v_image_mimetype varchar2(4000);
v_image_filename varchar2(4000);
v_image_content blob;
begin
/* provide image data for declared variables */
sys.HTP.init;
sys.OWA_UTIL.mime_header(v_image_mimetype, FALSE);
sys.HTP.p('Content-Length: ' || DBMS_LOB.getlength(v_image_content));
sys.HTP.p('Access-Control-Allow-Origin: *');
sys.HTP.p('Content-Disposition: filename="' || v_image_filename || '"');
sys.OWA_UTIL.http_header_close;
sys.WPG_DOCLOAD.download_file(v_file_row.FILE_CONTENT);
:status_code := 200;
end;