オブジェクトをダウンロードするリンクの追加
オブジェクトのダウンロード要求を処理するページを新規に作成します。このページは画面の表示には使われません。アプリケーションの開発画面よりページの作成を実行し、空白ページを選びます。
ページ番号として3、名前はDownload Object、ページ・モードは標準を選択します。ナビゲーションのブレッドクラム、ナビゲーションともにOFFにし、ページの作成を実行します。
作成したプロセスの識別の名前をdownloadObjectとし、識別のタイプにコードを実行を指定します。ソースの位置はローカル・データベース、PL/SQLコードとして以下を記述します。元のブログのコードは日本語の扱いに不備があったので、その部分を修正しています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare | |
l_request_url varchar2(32767); | |
l_content_type varchar2(32767); | |
l_content_length varchar2(32767); | |
l_response blob; | |
download_failed_exception exception; | |
begin | |
l_request_url := :G_BASE_URL || 'b/' | |
|| :P3_BUCKET_NAME || '/o/' | |
|| utl_url.escape(:P3_OBJECT_NAME, false, 'AL32UTF8'); | |
l_response := apex_web_service.make_rest_request_b( | |
p_url => l_request_url | |
, p_http_method => 'GET' | |
, p_credential_static_id => :G_OCI_WEB_CREDENTIAL | |
); | |
if apex_web_service.g_status_code != 200 then | |
raise download_failed_exception; | |
end if; | |
for i in 1..apex_web_service.g_headers.count | |
loop | |
if apex_web_service.g_headers(i).name = | |
'Content-Length' | |
then | |
l_content_length := | |
apex_web_service.g_headers(i).value; | |
end if; | |
if apex_web_service.g_headers(i).name = | |
'Content-Type' | |
then | |
l_content_type := | |
apex_web_service.g_headers(i).value; | |
end if; | |
end loop; | |
sys.htp.init; | |
if l_content_type is not null then | |
sys.owa_util.mime_header(trim(l_content_type), false); | |
end if; | |
sys.htp.p('Content-length: ' || l_content_length); | |
sys.htp.p('Content-Disposition: attachment; filename="' | |
|| utl_url.escape(:P3_OBJECT_NAME, false, 'AL32UTF8') || '"' ); | |
sys.htp.p('Cache-Control: max-age=3600'); -- if desired | |
sys.owa_util.http_header_close; | |
sys.wpg_docload.download_file(l_response); | |
apex_application.stop_apex_engine; | |
end; |
このコードの最後に
apex_application.stop_apex_engine;
の記載があります。downloadObjectプロセスはAPEXの標準的なページ処理を中断させているため、この後の処理は(どのような処理をページに追加していても)実行されません。
次にページ1のホーム画面をページ・デザイナで開き、オブジェクトの一覧にダウンロード・リンクを追加します。リージョンBucket Contentsの列の上でコンテキスト・メニューを開き、仮想列の作成を実行します。
リンクのターゲットは先ほど作成したDownload Objectのページなので、ターゲットのタイプはこのアプリケーションのページ、ページは3です。アイテムの設定として、P3_BUCKET_NAMEにはページ・アイテム&P1_BUCKET_NAME.を値として渡し、P3_OBJECT_NAMEにはレポートの出力である#NAME#を値として与えます。OKをクリックして、ターゲットを設定します。