Skip to main content

How to display image without page item reference in Oracle APEX

In this section, we'll learn how to display images in our Oracle APEX application without relying on a page item. This method is perfect for situations where we need to show an image directly from a database table column, a file, or a URL without first loading it into a specific page item. We'll cover how to achieve this using a dynamic action and a PL/SQL procedure, making our application more efficient and flexible. By the end of this tutorial, we'll be able to display images dynamically, improving the user experience and reducing page load times. This technique is useful for reports, dashboards, and custom user interfaces where a static page item isn't the best solution. You can vind a visual  solution  from here. STEP 1: Create a blank page. STEP 2: Create a region and set it properties like-          Name:                    Emp          Type:      ...

How to preview & download files from database or local directory in Oracle APEX

 It is common for Oracle APEX developers to need to handle files. This blog post will show you how to preview and download files stored in a database or a local directory. The most common approach in Oracle APEX is to store files directly in the database, typically within a BLOB (Binary Large Object) column. The primary reason is that this method simplifies data management and backup.

Sometimes, you may need to handle files stored directly on the server's file system rather than in the database. This is common for large files or when files are managed by other systems.

We will cover the basic steps and code snippets required for each method. Before approaching this tutorial, you must need to cover (preview image in large scale and save file into database/local directory) these two tutorial.

Step 1: Create a blank page and then create 3 regions. Set these three regions properties as like below-

Region-1:

    Name:         Tab Container

    Template:    Tabs Container    

Region- 2:

    Name:        From Database

    Type:        Interactive Report

    Source:

                Location:    Local Database

                Type:        SQL Query

                SQL Query:

                        SELECT employee_id

                            , first_name||' '||last_name AS emp_name

                            , job_id

                            , department_id

                            , salary

                           , email

                            , phone_number

                            , hire_date

                            , CASE file_format WHEN 'PDF' THEN

                                '<div class="rpt-container rpt-img" style="background-image: url(#APP_FILES#pdf.png)">'

                              WHEN 'IMAGE' THEN

                                '<div class="rpt-container rpt-img"><img style="width:100px; height:80px" src="'||apex_util.get_blob_file_src('P15_EMP_PHOTO', employee_id)||'">'

                              END ||'

                                    <div class="download">

                                       <div id="'||employee_id||'" style="margin-right:5px" onclick="downloadFile(this)"><span class="download-img fa fa-download"></span></div>

                                       <div id="'||employee_id||'" style="margin-left:5px" onclick="previewFile(this)"><span class="download-img fa fa-eye-slash"></span></div>

                                    </div>

                            </div>'   AS file_handle

                  FROM employees

                  WHERE storage_type = 'S2T'

Parent Region:    Tab Container

Slot:                    Tabs


Region- 3:

Name:        Local Directory

Type:         Interactive Report

Source:

                Location:    Local Database

                Type:        SQL Query

                SQL Query:

                                SELECT employee_id

                            , first_name||' '||last_name AS emp_name

                            , job_id

                            , department_id

                            , salary

                           , email

                            , phone_number

                            , hire_date

                            , CASE file_format WHEN 'PDF' THEN

                                '<div class="rpt-container rpt-img" style="background-image: url(#APP_FILES#pdf.png)">'

                              WHEN 'IMAGE' THEN

                                '<div class="rpt-container rpt-img"><img style="width:100px; height:80px" src="'http://localhost:9090/i/IMAGE_DIR/'||employee_id||'.'||file_type||">'

                              END ||'

                                    <div class="download">

                                       <div id="'||employee_id||'" style="margin-right:5px" onclick="downloadFile(this)"><span class="download-img fa fa-download"></span></div>

                                       <div id="'||employee_id||'" style="margin-left:5px" onclick="previewFile(this)"><span class="download-img fa fa-eye-slash"></span></div>

                                    </div>

                            </div>'   AS file_handle

                  FROM employees

                  WHERE storage_type = 'S2D'

Parent Region:    Tab Container

Slot:                    Tabs

Note: Replace local directory using you own files path.


Step 2: Create Ajax Callback Process to prepare the files for preview & download. 

    Name:    GET_FILETYPE

    Location:    Local Database

    PL/SQL Code:

                BEGIN

                    apex_json.open_array;

                    apex_json.open_object;

                    FOR i IN (

                        SELECT file_format

                            , storage_type

                            , file_type

                            , CASE storage_type WHEN 'S2T' THEN

                                apex_web_service.blob2clobbase64(emp_photo)

                              END file_src

                        FROM employees

                        WHERE employee_id = apex_application.g_x01

                    ) LOOP

                        apex_json.write('FILE_FORMAT', i.file_format);

                        apex_json.write('STORAGE_TYPE', i.storage_type);

                        apex_json.write('FILE_TYPE', i.file_type);

                        apex_json.write('FILE_SRC', i.file_src);

                    END LOOP;

                    apex_json.close_object;

                    apex_json.close_all;

                END;



Step 3: Create Javascript function in page level 'Function and Global Variable Declaration' section to preview and download files from database & local directory-

Open code editor and paste the below code-

function previewFile(file) {

    apex.message.clearErrors();

    var empId = file.id;

    apex.server.process(

        'GET_FILETYPE'

        , {

            x01: empId

        }

        , {

            dataType: 'json',

            success: function(pData) {

                var div = document.createElement('div');

                div.setAttribute('id', 'image-display');

                var spn = document.createElement('span');

                spn.innerText = 'x';

                var src;

                if (pData[0].FILE_FORMAT === 'IMAGE') {

                    var img = document.createElement('img');

                    var srcFile = file.closest('.download').previousElementSibling;

                    src = srcFile.getAttribute('src');

                    img.setAttribute('src', src);

                    div.appendChild(img);

                } else {

                    var iFrame = document.createElement('iframe');

                    if (pData[0].STORAGE_TYPE === 'S2D') {

                        src = 'http://localhost:9090/i/IMAGE_DIR/'+empId+'.'+pData[0].FILE_TYPE;

                    } else {

                        src = 'data:application/pdf;base64,'+pData[0].FILE_SRC ;

                    }

                    iFrame.setAttribute('src', src);

                    iFrame.setAttribute('height', '600px');

                    iFrame.setAttribute('width', '100%');

                    div.appendChild(iFrame);

                }

                div.appendChild(spn);

                document.body.appendChild(div);


                document.querySelector('#image-display').style.display = 'block';

                document.querySelector('#image-display span').onclick = () => {

                    document.querySelector('#image-display').remove();

                }

            }

        }

    )      

};


function downloadFile(file) {

    apex.message.clearErrors();

    var empId = file.id;

    apex.server.process(

        'GET_FILETYPE'

        , {

            x01: empId

        }

        , {

            dataType: 'json',

            success: function(pData) {

                var link = document.createElement('a');

                if (pData[0].STORAGE_TYPE === 'S2D') {

                    link.href = 'http://localhost:9090/i/IMAGE_DIR/'+empId+'.'+pData[0].FILE_TYPE;

                } else {

                    if (pData[0].FILE_FORMAT === 'IMAGE') {

                        link.href = 'data:image/'+pData[0].FILE_TYPE+';base64,'+pData[0].FILE_SRC;

                    } else {

                        link.href = 'data:application/'+pData[0].FILE_TYPE+';base64,'+pData[0].FILE_SRC;  

                    }

                }

                link.download = empId+'.'+pData[0].FILE_TYPE;

                link.click();

            }

        }

    )

}


Conclusion:

Both methods have their pros and cons. The database approach is generally recommended for its simplicity and robustness, but there is a little bit limitation of previewing large pdf file in this way while the local directory approach is better suited for specific use cases, such as handling very large files or integrating with other file-based systems. By understanding these techniques, you can choose the best method for your specific application requirements.

Happy developing!

                                

            

Comments

Post a Comment

Popular posts from this blog

How to integrate gmail and send email from Oracle APEX

Manually managing communications with vendors and customers in today's enterprise environment is a time-consuming and often inefficient process. Juggling emails, follow-ups, and notifications can quickly become overwhelming, leading to missed opportunities and delays. To streamline this critical aspect of business, it's essential to modernize your communication system. By integrating email directly into your business software, you can automate these interactions, ensuring timely and consistent communication without the manual effort. I have try to demonstrate step by step how to integrate gamil account with application in Oracle APEX. For better understanding, you can follow the email configuration system. Step 1: Create Access Control List (ACL). Connect your sys user and execute the below command- DECLARE     l_acl_path     VARCHAR2(3000); BEGIN     SELECT acl     INTO l_acl_path     FROM dba_network_acls     WHERE host = '*'...

How to send SMS from Oracle APEX

 In today’s digitalized business solution world, SMS notifications are one of the fastest ways to reach users. Whether it’s for OTP (One-Time Password), reminders, or alerts, integrating SMS with your Oracle APEX application can add huge value. In this guide, we’ll walk through different ways to send SMS from Oracle APEX . For better understand follow this  Tutorial in Youtube. Why Send SMS from Oracle APEX? Oracle APEX already provides built-in features for sending emails, but sometimes email isn’t enough. SMS can be useful for: OTP & Two-factor Authentication Order Confirmations Text Notifications Promotional Messages Process to Send SMS in Oracle APEX 1. Register with Third Party SMS Gateway Most SMS providers (like Twilio, Nexmo, or local telecom gateways) expose a REST API to send SMS. Oracle APEX supports REST integrations, so you can call these APIs directly. Steps: Choose an SMS provider (e.g., Twilio, Nexmo, Clickatell, or your local...