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!

How to add flies into a SERVER DB through APEX Front End?

YOGESH SJun 10 2024

Here is the answer, I had a requirement to handle files into server DB through APEX front end as a developer made many R&D's and with the help of my colleagues we found a solution to full-fill the requirement.

Let we start,
STEP 1 : Create a Table with required columns.
STEP 2 : Create a Interactive report with a form.
STEP 3 : FILE UPLOAD PROCESS

          3.1 : Store the File Upload page item's blob content into APEX\_APPLICATION\_TEMP\_FILES.

DECLARE 
v_filecontent BLOB; 
v_foldername VARCHAR2(200) := 'YOUR_DIRECTORY'; 
v_filename VARCHAR2(200); 
v_charset VARCHAR2(255); 
l_mimetype VARCHAR2(200); 
buffer RAW(1024); 
offset PLS_INTEGER := 1; 
filelength PLS_INTEGER; 
amount PLS_INTEGER := 1024; 
fhandle utl_file.file_type; 
BEGIN 
-- SELECT STATEMENT TO FETCH YOUR CURRENT ATTACHED FILE DETAILS. 
SELECT 
f.blob_content, 
f.mime_type, 
f.filename 
INTO 
v_filecontent, 
l_mimetype, 
v_filename 
FROM 
apex_application_temp_files f 
WHERE 
f.name = your_blob_page_item; 
-- QYERY TO PERFORM INSERT AND UPDATE ACTION ON TABLE LEVEL WHEN INSERTING AND UPDATING. 
IF your_blob_page_item IS NOT NULL THEN 
IF your_table_primary_key IS NULL THEN 
INSERT INTO your_table_name ( 
your_filename_column, 
your_mimetype_column 
) VALUES ( 
v_filename, 
l_mimetype 
); 
END IF; 

IF your_table_primary_key IS NOT NULL THEN 
UPDATE your_table_name 
SET 
your_filename_column = v_filename, 
your_mimetype_column = l_mimetype 
WHERE 
file_id = your_table_primary_key; 
END IF; 
-- FETCHING FILE LENGTH AND FILE NAME. 
filelength := dbms_lob.getlength(v_filecontent); 
fhandle := utl_file.fopen(v_foldername, v_filename, 'wb'); -- OPENING THE FILE 
-- LOOP TO READ ENTIRE DATA IN THE FILE AND UPLOADING INTO DIRECTORY USING PUT. 
LOOP 
EXIT WHEN offset > filelength; 
dbms_lob.read(v_filecontent, amount, offset, buffer); 
utl_file.put_raw(fhandle, buffer, true); 
offset := offset + amount; 
END LOOP; 

utl_file.fclose(fhandle);-- CLOSING THE FILE 
END IF; 

EXCEPTION 
WHEN OTHERS THEN 
IF utl_file.is_open(fhandle) THEN 
utl_file.fclose(fhandle); 
END IF; 
RAISE; 
END;

DECLARE 
l_file_loc BFILE; 
l_exists NUMBER; 
lv_dir VARCHAR2(100) := 'YOUR_DIRECTORY'; 
lv_filename VARCHAR2(100); 
BEGIN 
-- TO FETCH FILE DETAILS. 
SELECT 
f.filename 
INTO lv_filename 
FROM 
apex_application_temp_files f 
WHERE 
f.name = your_blob_page_item; 
-- TO CHECK THE PRESENCE OF FILE IN DIRECTORY. 
l_file_loc := bfilename(upper(lv_dir), lv_filename); 
l_exists := dbms_lob.fileexists(l_file_loc); 
IF l_exists = 1 THEN 
RETURN 'File already exists.'; 
ELSIF l_exists = 0 THEN 
DBMS_OUTPUT.PUT_LINE('File NOT Exists'); 
END IF; 
EXCEPTION 
WHEN utl_file.invalid_path THEN 
RETURN 'ERROR INVALID PATH: File NOT Exists'; 
WHEN utl_file.invalid_operation THEN 
RETURN 'ERROR INVALID OPERATION: File NOT Exists'; 
END;

<script> 

// FUNCTION TO SET FILE_NAME AND PRIMARY_KEY FOR DELETE. 
function del_setId(p_detID,p_name) 
{ 
var pg_name = $v('YOUR_FILE_NAME_ITEM'); 
if (p_name !== pg_name ) { 
apex.message.alert('Please download the file and then proceed to delete.'); 
} 
else{ 
$s('YOUR_PIMARY_KEY_ITEM',p_detID); // TO SET THE VALUE TO ITEM. 
var confirmtext= 'Are you sure you want to delete the file '+p_name+'?'; 
apex.confirm(confirmtext, 'DELETE'); 
} 
} 
// FUNCTION TO SET FILE_NAME AND PRIMARY_KEY FOR DOWNLOAD. 
function download_setId(p_detID,p_name) 
{ 
$s('YOUR_PIMARY_KEY_ITEM', p_detID); 
$s('YOUR_FILE_NAME_ITEM',p_name); 
var confirmtext= 'Are you sure you want to download the file '+p_name+'?'; 
apex.confirm(confirmtext, 'DOWNLOAD');} 
</script>

DECLARE 
v_directory_name VARCHAR2(100) := 'YOUR_DIRECTORY'; 
v_filename VARCHAR2(100); 
BEGIN 
SELECT 
attach_filename 
INTO v_filename 
FROM 
your_table_name 
WHERE 
file_id = your_primary_key; 

utl_file.fremove(v_directory_name, v_filename); 
END; 

DECLARE 
v_file utl_file.file_type; 
v_file_name VARCHAR2(100); 
l_blob_content BLOB; 
l_mime_type VARCHAR2(100); 
l_directory VARCHAR2(100) := 'YOUR_DIRECTORY'; 
l_chunk_size NUMBER := 32767; 
BEGIN 
SELECT 
attach_filename, 
attach_mimetype 
INTO 
v_file_name, 
l_mime_type 
FROM 
your_table_name 
WHERE 
file_id = :your_primary_key_item; 

v_file := utl_file.fopen(l_directory, v_file_name, 'r', 32767); 
IF utl_file.is_open(v_file) THEN 
-- Read the content of the file into a BLOB 
dbms_lob.createtemporary(l_blob_content, true); 
-- Chunk of raw data read from file 
LOOP 
DECLARE 
l_raw_content RAW(32767); 
BEGIN 
utl_file.get_raw(v_file, l_raw_content, l_chunk_size); 
IF l_raw_content IS NULL THEN 
-- Exit loop if end of file reached 
EXIT; 
END IF; 
dbms_lob.append(l_blob_content, l_raw_content); 
EXCEPTION 
WHEN no_data_found THEN 
-- Exit loop if end of file reached 
EXIT; 
END; 
END LOOP; 
-- Close the file 
utl_file.fclose(v_file); 
-- Download the file 
sys.htp.init; 
sys.owa_util.mime_header(l_mime_type, false); 
sys.htp.p('Content-Length: ' 
|| sys.dbms_lob.getlength(l_blob_content)); 

sys.htp.p('Content-Disposition: attachment; filename="' 
|| v_file_name 
|| '"'); 
sys.htp.p('Cache-Control: max-age=0'); 
sys.owa_util.http_header_close; 
sys.wpg_docload.download_file(l_blob_content); 

-- Reset page items 
your_download_flag := 0; 
END IF; 
END;

That's all now you can easily manipulate files into server directory easily.

Kindly suggest if there is any enhancement or extra add on for the above solution and also if there is any corrections or issues.

Thanks

Yogesh S.

Comments
Post Details
Added on Jun 10 2024
0 comments
237 views