Search This Blog

Monday, November 13, 2023

Multiple File Download into a ZIP File in Oracle Apex

*** Please Follow My Blog to Inspire Me More Posting. ***


Step 1. Create a Classic Report and add a checkbox 
------------------------------------------------------------

SELECT 
ID,
APEX_ITEM.display_and_save (02,ID)ID,
FILE_NAME,
FILE_MIMETYPE,
FILE_CHARSET,
FILE_LASTUPD_DTTM,
FILE_UPLOAD_DATE,
FILE_UPLOAD_STATUS,
apex_item.checkbox(01,'#ROWNUM#') CheckBox
FROM TABLE
WHERE 1=1 AND
ID=:P5_ID; 


Step 2. Create a process 
--------------------------------


declare

var_zip blob; 
var number ;

begin

-- Create/clear the ZIP collection
apex_collection.create_or_truncate_collection(
p_collection_name => 'ZIP');

if APEX_APPLICATION.G_F01.COUNT < 1 then 
raise_application_error(-20001,'Please Select to Download!');
end if;


for i in 1..APEX_APPLICATION.G_F01.COUNT  loop -- Loop through all the files in the database


 var:=APEX_APPLICATION.G_F01(i);

   begin

        for var_file in (select FILE_BLOB , FILE_NAME
                         from TABLE
                         where 1=1
                         AND ID=UPPER(APEX_APPLICATION.G_F02(var)))
        loop      
            -- Add each file to the var_zip file
            apex_zip.add_file(
                p_zipped_blob => var_zip,
                p_file_name   => var_file.FILE_NAME,
                p_content     => var_file.FILE_BLOB );
        end loop;  

    exception when no_data_found then
        -- If there are no files in the database, handle error
        raise_application_error(-20001, 'No Files found!');
    end;

    end loop;

    -- Finish creating the zip file (var_zip)
    apex_zip.finish(
        p_zipped_blob => var_zip);

    -- Add var_zip to the blob column of the ZIP collection
    apex_collection.add_member(
        p_collection_name => 'ZIP',
        p_blob001         => var_zip);

end;



Step-3  Create a Ajax Callback Process  with this name ---download_zip_file  
------------------------------------------------------------------------------------------------------


declare

  var_mimetype varchar2(50) := 'application/zip';
  var_name varchar2(100) := to_char(current_date, 'DD-MON-YYYY') || '_File.zip';
  var_blob blob;

begin

    -- Get the BLOB from the ZIP collection

    select blob001 into var_blob from apex_collections where collection_name = 'ZIP' and seq_id = 1;

    sys.htp.init;
    sys.owa_util.mime_header( var_mimetype, FALSE );
    sys.htp.p('Content-length: ' || sys.dbms_lob.getlength( var_blob));
    sys.htp.p('Content-Disposition: attachment; filename="' || var_name || '"' );
    sys.htp.p('Cache-Control: max-age=3600');  -- tell the browser to cache for one hour, adjust as necessary
    sys.owa_util.http_header_close;
    sys.wpg_docload.download_file( var_blob );

    apex_application.stop_apex_engine;

    apex_collection.truncate_collection(p_collection_name => 'ZIP');
    
exception when apex_application.e_stop_apex_engine then

    NULL;
end; 


Step 4. Create a Branche on After Processing
---------------------------------------------------------------

Target-> Type-> Page in this application 
page -> same page 5

Advanced-> Request-> APPLICATION_PROCESS=download_zip_file  -- download_zip_file is the Ajax Callback process (Name must be same)


No comments:

Post a Comment

Restrict File Upload by File Type in Oracle Apex

If you want to restrict file upload by file type/extension/format you can follow the below steps.  Goto File Browser Item --> Advanced --...