Thursday, 24 January 2013

File Upload Download in Apex


FILE UPLOADING AND DOWNLOADING:
CREATE TABLE  "XXP_UPLOAD1"("ID" NUMBER(10,0),"FILENAME" VARCHAR2(400),     "MIMETYPE" VARCHAR2(400),"FILECONTENT" BLOB,"DOC_SIZE" NUMBER(10,0),
        "NAME" VARCHAR2(400),"DESCRIPTION" VARCHAR2(400),
        "FNAME" VARCHAR2(400),
         CONSTRAINT "XXP_UPLOAD1_PK" PRIMARY KEY ("ID") ENABLE  )
-------Table created-----------
1>create blank page in apex, add html region, add two page item to accept name and description, one page item for browsing, create one submit/upload Button,in the same region create classic report. then run the application. when you run it look like this

 3>create one blank page with no tabs to assigned to it, create one page item button on it (to accept id )
4>in branching processing create download process (download_my_file) and pass id to it.
Right click->create process->select process name->select BRANCH TYPE=BRANCH TO PLSQL PROCEDURE
->WRITE PROCEDURE NAME  ie download_my_file(:P1_ID) (in my application)
5> run the application
ie download_my_file code is given below:
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 MIMETYPE, FILECONTENT, fname,DBMS_LOB.GETLENGTH(FILECONTENT)
INTO v_mime,lob_loc,v_file_name,v_length
FROM xxp_upload1
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)|| '"');
-- close the headers
owa_util.http_header_close;
-- download the BLOB
wpg_docload.download_file( Lob_loc );
END download_my_file;

No comments:

Post a Comment