Posted by : Akshay Patil
Monday, 19 October 2015
Below PL/SQL Block will help you to download file from specific directory path to your browser.
First you need to register the directory path of file with oracle using below syntax.
DECLARE
V_Amount Integer;
V_Src_Loc Bfile;
v_b BLOB;
BEGIN
DBMS_LOB.CREATETEMPORARY(V_B,true);
v_src_loc := BFILENAME('MY_DIR', 'myfile.xls');
--MY_DIR is the directory registered with a path where file is present.
Sys.Dbms_Lob.Open(V_Src_Loc, Dbms_Lob.Lob_Readonly);
V_Amount := SYS.Dbms_Lob.Getlength(V_Src_Loc);
htp.flush;
htp.init;
-- Set the MIME type
OWA_UTIL.mime_header ('application/octet', FALSE);
htp.p('Content-length: ' || V_Amount );
HTP.p('Content-Disposition: attachment; filename="myfile.xls"');
OWA_UTIL.http_header_close;
Sys.Dbms_Lob.Loadfromfile(V_B, V_Src_Loc, V_Amount);
Sys.Dbms_Lob.Close(V_Src_Loc);
UTL_FILE.FREMOVE(location=>'MY_DIR',filename=>'myfile.xls');
wpg_docload.download_file(V_B);
apex_application.stop_apex_engine;
exception
when others then
htp.p('Error '||SQLERRM);
END;
First you need to register the directory path of file with oracle using below syntax.
CREATE [OR REPLACE] DIRECTORY directory_name AS 'path_name';
where:
directory_name: Database-unique directory name.
path_name: Operating system directory path (note that if the OS is case-sensitive, the name must be exact)
Now,give read,write access to database user for created directory.
grant read, write on directory directory_name to database_schema
DECLARE
V_Amount Integer;
V_Src_Loc Bfile;
v_b BLOB;
BEGIN
DBMS_LOB.CREATETEMPORARY(V_B,true);
v_src_loc := BFILENAME('MY_DIR', 'myfile.xls');
--MY_DIR is the directory registered with a path where file is present.
Sys.Dbms_Lob.Open(V_Src_Loc, Dbms_Lob.Lob_Readonly);
V_Amount := SYS.Dbms_Lob.Getlength(V_Src_Loc);
htp.flush;
htp.init;
-- Set the MIME type
OWA_UTIL.mime_header ('application/octet', FALSE);
htp.p('Content-length: ' || V_Amount );
HTP.p('Content-Disposition: attachment; filename="myfile.xls"');
OWA_UTIL.http_header_close;
Sys.Dbms_Lob.Loadfromfile(V_B, V_Src_Loc, V_Amount);
Sys.Dbms_Lob.Close(V_Src_Loc);
UTL_FILE.FREMOVE(location=>'MY_DIR',filename=>'myfile.xls');
wpg_docload.download_file(V_B);
apex_application.stop_apex_engine;
exception
when others then
htp.p('Error '||SQLERRM);
END;