Friday, 19 April 2024

Save Image in Directory and Show Image from Directory with out Saving the Image in Database.

 Save Image in Directory and Show Image from Directory with out Saving the Image in Database.


Create a Procedure as Following..

CREATE OR REPLACE PROCEDURE APEX.blob_to_file_from_apex (p_blob      IN OUT NOCOPY BLOB,
                                          p_dir       IN  VARCHAR2,
                                          p_filename  IN  VARCHAR2)
AS
  l_file      UTL_FILE.FILE_TYPE;
  l_buffer    RAW(32767);
  l_amount    BINARY_INTEGER := 32767;
  l_pos       INTEGER := 1;
  l_blob_len  INTEGER;
BEGIN
  l_blob_len := DBMS_LOB.getlength(p_blob);
  
  -- Open the destination file.
  l_file := UTL_FILE.fopen(p_dir, p_filename,'WB', 32767);

  -- Read chunks of the BLOB and write them to the file until complete.
  WHILE l_pos <= l_blob_len LOOP
    DBMS_LOB.read(p_blob, l_amount, l_pos, l_buffer);
    UTL_FILE.put_raw(l_file, l_buffer, TRUE);
    l_pos := l_pos + l_amount;
  END LOOP;
  
  -- Close the file.
  UTL_FILE.fclose(l_file);
  
EXCEPTION
  WHEN OTHERS THEN
   -- Close the file if something goes wrong.
    IF UTL_FILE.is_open(l_file) THEN
      UTL_FILE.fclose(l_file);
    END IF;
    RAISE;
/* WHEN UTL_FILE.invalid_operation THEN dbms_output.PUT_LINE('cannot open file invalid name');
WHEN UTL_FILE.read_error THEN dbms_output.PUT_LINE('cannot be read');
WHEN no_data_found THEN dbms_output.PUT_LINE('end of file');

UTL_FILE.fclose(l_file);
 RAISE;*/
END ;
/

---------------------------------------------------------------------------------------

CREATE OR REPLACE procedure insert_pic_path(maindoc varchar2,userid number,pic_path varchar2,lineid number) is
begin
insert into pics_path(main_doc,user_id,p_path,line_id)
VALUES (maindoc,userid,pic_path,lineid);
COMMIT;
END;
/

---------------------------------------------------------------------------------------

DECLARE
    l_blob BLOB;
BEGIN
insert_pic_path(:P554_VNO ,:USER_ID,:P554_VNO||'_001' , seq_pic_path.nextval);
COMMIT;

    SELECT BLOB_CONTENT
    INTO l_blob
    FROM apex_application_temp_files
    WHERE name = :P554_NEW;
    blob_to_file_FROM_APEX (
        p_blob => l_blob,
        p_dir => 'VNO_IUS',
        p_filename => :P554_VNO||'_001'||'.JPG');  

END;

---------------------------------------------------------------------------------------

Now Create a Function as Following.

CREATE OR REPLACE FUNCTION APEX.GET_image_from_directory(imgname VARCHAR2) RETURN BLOB IS  
 l_blob  blob;
        l_bfile bfile;
begin
       DBMS_LOB.CREATETEMPORARY(l_blob, TRUE);

      l_bfile := bfilename( 'VNO_IUS', imgname||'.jpg' );
      dbms_lob.fileopen( l_bfile );

      dbms_lob.loadfromfile( l_blob, l_bfile,
                               dbms_lob.getlength( l_bfile ) );
      dbms_lob.fileclose( l_bfile );
        return l_blob;
        END;
/

---------------------------------------------------------------------------------------

No comments:

Post a Comment

Check Internet Connectivity on Local System

if (!navigator.onLine) {     showOfflineAlert(); } else {     // Set a strict 3-second network timeout threshold     const controller = new ...