Downloading a File From a Custom Table

Problem

Downloading a file from a blob stored in a custom table is not well documented, or the solutions don't work very well.

Solution

I found a solution that does work well, except for one thing that needed to change because the downloaded file name was always "f.xml" or "f.txt" or "f.doc".
The solution is documented here:


Hihglighted is the code that I changed to get the correct filename.

CREATE OR REPLACE PROCEDURE download_my_file(p_file IN NUMBER) AS
v_mime VARCHAR2(48);
v_length NUMBER;
v_file_name VARCHAR2(2000);
Lob_loc BLOB;
BEGIN
SELECT FILE_MIMETYPE, FILE_BLOB, FILENAME            ,DBMS_LOB.GETLENGTH(FILE_BLOB)
INTO v_mime,lob_loc,v_file_name,v_length
FROM XXGNS_IG_FILES
WHERE id = p_file;
--
-- set up HTTP header
--
-- use an NVL around the mime type and
-- if it is a null set it to application/octect
-- application/octect may launch a download window from windows
owa_util.mime_header( nvl(v_mime,'application/octet'),
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="'||REPLACE(REPLACE(substr(v_file_name,instr(v_file_name,'/')+1),chr(10),NULL),chr(13)

,NULL)|| '"');*/
HTP.P('Content-Disposition: attachment; filename="' || v_file_name|| '"');
-- close the headers
owa_util.http_header_close;
-- download the BLOB
wpg_docload.download_file( Lob_loc );
END download_my_file;

Acknowledgment

Comments