Overview

This document talks about uploading Blob Content without Page Submit. This source code used in this document has been taken and modified from the following website,

https://apexplained.wordpress.com/2016/09/12/chunked-multi-file-upload-with-ajax/.

Technologies and Tools Used

The following technologies have been used to achieve this functionality,

  • PLSQL
  • JavaScript
  • AJAX

Use Case

Usually in Apex, uploading the Blob Content requires page submit. This is because during the page submit, APEX implicitly stores the uploaded file into the APEX_APPLICATION_TEMP_FILES (which is the temporary table) at first and then it can be moved to the target table. But in this blog, we are going to learn how to upload the File without Page Submit.

Architecture

Let us see step by step,

Step 1: Create two items, one to store the primary key column value and another one is to upload the file (File Browse item) on the target page

e.g.
Item Name – P1_PK_COL
Type – Hidden (based on your requirement)
Item Name – P1_FILE_BROWSE
Type – File Browse

Step 2: Create a Button to trigger the upload action after the file has been chosen. Remember that the button should not submit the page during button click. To change the button action, go to the action property of the button and set its Action to Defined by Dynamic Action.

e.g.
Button Name – UPLOAD

Step 3: Go to the page header property and navigate to the JavaScript section, then place the following code in the Function and Global Variable Declaration,

var fileInputElem = document.getElementById(‘P1_FILE_BROWSE’);
var fileIndex = 0;
var lSpinner$;

// builds a js array from long string
function clob2Array(clob, size, array) {
loopCount = Math.floor(clob.length / size) + 1;
for (var i = 0; i < loopCount; i++) {
array.push(clob.slice(size * i, size * (i + 1)));
}
return array;
}

// converts binaryArray to base64 string
function binaryArray2base64(int8Array) {
var data = “”;
var bytes = new Uint8Array(int8Array);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
data += String.fromCharCode(bytes[i]);
}
return btoa(data);
}
// a function to upload file
function uploadFile(pFileIndex) {
var file = fileInputElem.files[pFileIndex];
var reader = new FileReader();

reader.onload = (function(pFile) {
return function(e) {
if (pFile) {
var base64 = binaryArray2base64(e.target.result);
var f01Array = [];
f01Array = clob2Array(base64, 30000, f01Array);

apex.server.process(
‘UPDATE_BLOB_CONTENT’, {
x01: file.name,
x02: file.type,
x03: $v(‘P1_PK_COL’),
f01: f01Array
}, {
dataType: ‘text’,
success: function(data) {
apex.message.clearErrors();
lSpinner$.remove();
if (data == ‘S’) {
apex.message.showPageSuccess(“Uploaded Successfully!!!”);
} else {
apex.message.showErrors([{
type: “error”,
location: “page”,
message: ‘Error in uploading file.’,
unsafe: false
}]);
}
}
}
);
}
}
})(file);
reader.readAsArrayBuffer(file);
}

Step 4: Then, Create an AJAX callback process on your target page as mentioned below,

e.g
Name – UPDATE_BLOB_CONTENT
Point – Ajax Callback
PL/SQL Code –

DECLARE
l_blob BLOB;
l_filename VARCHAR2 (200);
l_mime_type VARCHAR2 (200);
l_token VARCHAR2 (32000);
lv_index NUMBER := 0;
lv_status VARCHAR2 (4000);
lv_pk_val NUMBER;
BEGIN
l_filename := apex_application.g_x01;
l_mime_type := NVL (apex_application.g_x02, ‘application/octet-stream’);
l_pk_val := apex_application.g_x03;
DBMS_LOB.createtemporary (l_blob, FALSE, DBMS_LOB.SESSION);

FOR i IN 1 .. apex_application.g_f01.COUNT
LOOP
l_token := wwv_flow.g_f01 (i);

IF LENGTH (l_token) > 0
THEN
DBMS_LOB.append
(dest_lob => l_blob,
src_lob => to_blob
(UTL_ENCODE.base64_decode
(UTL_RAW.cast_to_raw (l_token)
)
)
);
END IF;
END LOOP;

IF DBMS_LOB.getlength (l_blob) IS NOT NULL
THEN
UPDATE table_name
SET blob_content = l_blob,
mime_type = l_mime_type,
filename = l_filename
WHERE primary_key_col = l_pk_val;

HTP.prn (‘S’);
END IF;
EXCEPTION
WHEN OTHERS
THEN
HTP.prn (‘E’);
END;

Step 5: Finally, Create a dynamic action on click of the upload button as mentioned below,

e.g.
Name – On click of Upload
Event – Click
Selection Type – Button
Button – UPLOAD
Client Side Condition :
Type – JavaScript expression
Value – fileInputElem.files.length != 0
Action – Execute JavaScript Code
Settings – uploadFile(fileIndex);

Step 6: Save the changes of the page and Run the Page to test the upload process.

Recent Posts

Start typing and press Enter to search