Skip to content

Instantly share code, notes, and snippets.

@pshapoval
Last active December 26, 2015 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pshapoval/7182999 to your computer and use it in GitHub Desktop.
Save pshapoval/7182999 to your computer and use it in GitHub Desktop.
Generate correct MIME header for FS file that allow browser download such file.
DECLARE
l_filename BFILE;
v_export_dir VARCHAR2(2000);
v_length integer;
l_blob blob;
des_offset number := 1;
src_offset number := 1;
x number;
BEGIN
system_run('/path/to/report/generation/script.sh -r '); /*Run report generator script*/
l_filename := bfilename('EXPORT_DIR','/expected_report_file_name.pdf'); /*Please create EXPORT_DIR in Oracle before use*/
v_length := dbms_lob.getlength(l_filename);
DBMS_LOB.createtemporary(l_blob, FALSE);
dbms_lob.open(l_filename, dbms_lob.lob_readonly);
DBMS_LOB.LOADBLOBFROMFILE(dest_lob=>l_blob,
src_bfile=>l_filename,
amount=>v_length,
dest_offset=>des_offset,
src_offset=>src_offset);
owa_util.mime_header('application/pdf', FALSE );
-- set the size so the browser knows how much to download
htp.p('Content-length: ' || v_length);
-- the filename will be used by the browser if the users does a save as
htp.p('Content-Disposition: attachment; filename="expected_report_file_name.pdf"');
-- close the headers
owa_util.http_header_close;
-- download the BLOB
wpg_docload.download_file(l_blob);
DBMS_LOB.freetemporary(l_blob);
EXCEPTION WHEN OTHERS THEN
DBMS_LOB.freetemporary(l_blob);
RAISE;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment