Skip to main content

Template handler displaying image

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.

handler features

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

Requirements​

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

Handler definition​

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

Handler parameters​

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.

Handler Source​

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;

Handler Output​

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.

Snippet straming file content into the browser buffer
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;