Ticker

6/recent/ticker-posts

How to Upload File to Directory in Oracle APEX - Javainhand Tutorial

Oracle APEX Tutorial - Upload Images Into Local Directory in Oracle Apex

In this Oracle APEX Tutorial, I will explain to you How to Upload File to Directory in Oracle APEX.

So I have followed some steps for the oracle apex upload file into the directory

How to Upload File to the Directory in Oracle APEX?

Step 1:- Create a Folder into your local system I have created an image folder into my E:\images. The below screenshot will help you.

Oracle APEX Tutorial - Upload Images Into Local Directory or Folder in Oracle Apex

Step 2: Now Open CMD and connect with sys user and run bellow script:
create directory upload_image_dir as 'E:\upload_images';
grant read on directory upload_image_dir  to anonymous;
grant read on directory upload_image_dir  to admin;
Step 3: Create a page item with the file browse option which will use for uploading images.

Oracle APEX Tutorial - Upload Images Into Local Directory or Folder in Oracle Apex
Step 4:Create Page process for uploading the file into a directory as per the below screenshot and code:

Oracle APEX Tutorial - Upload Images Into Local Directory or Folder in Oracle Apex
DECLARE
V_IMAGE APEX_APPLICATION_TEMP_FILES.BLOB_CONTENT%TYPE;
V_FILENAME APEX_APPLICATION_TEMP_FILES.FILENAME%TYPE;
BEGIN
BEGIN
SELECT BLOB_CONTENT,FILENAME INTO V_IMAGE,V_FILENAME
FROM APEX_APPLICATION_TEMP_FILES
WHERE UPPER(NAME)=UPPER(:P7_IMAGE);
EXCEPTION WHEN NO_DATA_FOUND THEN
V_IMAGE:=NULL;
V_FILENAME:=NULL;
WHEN OTHERS THEN
V_IMAGE:=NULL;
V_FILENAME:=NULL;
END;

IF V_IMAGE IS NOT NULL AND V_FILENAME IS NOT NULL THEN
SAVEFILE(V_IMAGE,'UPLOAD_IMAGE_DIR',V_FILENAME);
ELSE
APEX_ERROR.ADD_ERROR (
 p_message  => 'Image Not Upload',
p_display_location => apex_error.c_inline_in_notification );
END IF;
END;

Note: Directory name should be in an upper case like "UPLOAD_IMAGE_DIR".

Step 5: Now finally create the database procedure for uploading a file into a local directory.

create or replace PROCEDURE SaveFile(
     FileContent IN OUT NOCOPY BLOB
   , FolderName IN VARCHAR2
   , FileName IN VARCHAR2)
IS
    BUFFER RAW(1024);
    OFFSET PLS_INTEGER := 1;
    FileLength PLS_INTEGER;
    amount PLS_INTEGER := 1024;
    fhandle UTL_FILE.FILE_TYPE;
BEGIN
    FileLength := DBMS_LOB.GETLENGTH(FileContent);
    fhandle := UTL_FILE.FOPEN(FolderName, FileName, 'wb');         
    LOOP
        EXIT WHEN OFFSET > FileLength;
        DBMS_LOB.READ(FileContent, amount, OFFSET, BUFFER);
        UTL_FILE.PUT_RAW(fhandle, BUFFER, TRUE);
        OFFSET := OFFSET + amount;
    END LOOP;
    UTL_FILE.FCLOSE (fhandle);
EXCEPTION
    WHEN OTHERS THEN
        IF UTL_FILE.IS_OPEN(fhandle) THEN
            UTL_FILE.FCLOSE(fhandle);
        END IF;
    RAISE;
END SaveFile;


If You Like This Tutorial Please Like Share And Comment.

Post a Comment

1 Comments

If you have any doubts, Please let me know