APEX: Clickable icon on an interactive report, which downloads a file

Problem

You want an icon that you can click on from an interactive report, that downloads a file.

Solution

Firstly, create your columns in your interactive report query

select to_char(RICEW_NUMBER,'9990.00')||' - '||ricew_name ricew,
       md50.ricew_attachment_id md50_ricew_attachment_id,
       md50.document_comment md50_document_comment,
       md50.created_by md50_created_by,
       md50.created_date md50_created_date,
       md50.file_type||'.png' md50_file_type,
        apex_util.prepare_url('f?p=' || :app_id || ':55:' || :app_session || '::NO:RP,5:P55_BRANCH_ID,P55_ATTACHMENT_ID:' || :app_branch_id || ',' || md50.ricew_attachment_id) md50_download_link
from....

On the column "MD50_RICEW_ATTACHMENT_ID", set the attributes as follows.
Type: Link
Target > Type: URL
Target > URL: #MD50_DOWNLOAD_LINK#
Link Text: <p align="middle">Document Uploaded: #MD50_CREATED_DATE#</p> <p align="middle">Uploaded By: #MD50_CREATED_BY#</p> <p align="middle">Last Comment: #MD50_DOCUMENT_COMMENT#</p><p align="middle"><img src="#APP_IMAGES##MD50_FILE_TYPE#"  width="75px" height="75px" align="middle"></p>
Link Attributes: target="_blank"
Escape characters: No

Why these values are important:
Type: Tells the browser that this is a clickable item
Target > Type: This allows us to send the browser to a URL that we have created in our query
Target > URL: i.e (apex_util.prepare_url('f?p=...)
Link Text: This is optional, but it displays info about the file that we are going to download. It uses values that we retrived in our query
Link Attributes: This allows us to continue our session
Escape characters: It reads all the special characters and works out how to display them.

A note about images for the icon:
My query works out what image I need. Example 'doc,png' which is returned by MD50_FILE_TYPE.
You will have to have uploaded these images beforedhand with the exact filenames under static application files.
Like this:



Secondly, you need to create your download page, mine is page 55, as you can see from the target URL

The page will have a download process and some hidden fields to accept your parameters.


Here is the code for the Download File.
begin
  ds_repository_handler_pkg.download_attachment(:p55_branch_id,
                                          :P55_ATTACHMENT_ID);
end;   

Lastly, you need some code to go and fetch your file from the database
Here is the download_attachment procedure.
procedure download_attachment(p_branch_id number,
                        p_ricew_attachment_id number) is
                        
  v_file_name      ds_component_files_header.file_name%type;
  v_mime_type      ds_component_files.mime_type%type;
  v_component_file ds_component_files.component_file%type;
  
begin
  select file_name, 
       mime_type , 
       attachment_file
  into v_file_name,
       v_mime_type,
       v_component_file           
  from apex_ebs_extension.DS_RICEW_ATTACHMENTS
  where branch_id = p_branch_id
  AND ricew_attachment_id = p_ricew_attachment_id;
               
  sys.htp.init;
  sys.owa_util.mime_header(v_mime_type, false);
  sys.htp.p('Content-length: ' || sys.dbms_lob.getlength(v_component_file));
  sys.htp.p('Content-Disposition: attachment; filename="'||v_file_name||'"' );
  sys.htp.p('Cache-Control: max-age=3600');
  sys.owa_util.http_header_close;
  sys.wpg_docload.download_file(v_component_file);
     
  apex_application.stop_apex_engine;

exception
  when no_data_found then
    null;
end;

RESULT
Clicking on the icon, initiates the download


Comments