Next we create a package specification which will contain all the necessary code:CREATE TABLE documents ( name VARCHAR2(256) UNIQUE NOT NULL, mime_type VARCHAR2(128), doc_size NUMBER, dad_charset VARCHAR2(128), last_updated DATE, content_type VARCHAR2(128), blob_content BLOB ) /
Then the package body:CREATE OR REPLACE PACKAGE document_api AS PROCEDURE upload (file IN VARCHAR2); PROCEDURE download; PROCEDURE download (file IN VARCHAR2); END; / SHOW ERRORS
CREATE OR REPLACE PACKAGE BODY document_api AS
-- ----------------------------------------------------------------------------
PROCEDURE upload (file IN VARCHAR2) AS
-- ----------------------------------------------------------------------------
l_real_name VARCHAR2(1000);
BEGIN
HTP.htmlopen;
HTP.headopen;
HTP.title('File Uploaded');
HTP.headclose;
HTP.bodyopen;
HTP.header(1, 'Upload Status');
l_real_name := SUBSTR(file, INSTR(file, '/') + 1);
BEGIN
-- Delete any existing document to allow update.
DELETE FROM documents
WHERE name = l_real_name;
-- Update the prefixed name with the real file name.
UPDATE documents
SET name = l_real_name
WHERE name = file;
HTP.print('Uploaded ' || l_real_name || ' successfully.');
EXCEPTION
WHEN OTHERS THEN
HTP.print('Upload of ' || l_real_name || ' failed.');
HTP.print(SQLERRM);
END;
HTP.br;
-- Create some links to demonstrate URL downloads.
HTP.br;
HTP.print('URL Downloads:');
HTP.br;
FOR cur_rec IN (SELECT name FROM documents) LOOP
HTP.anchor('docs/' || cur_rec.name, 'docs/' || cur_rec.name);
HTP.br;
END LOOP;
-- Create some links to demonstrate direct downloads.
HTP.br;
HTP.print('Direct Downloads:');
HTP.br;
FOR cur_rec IN (SELECT name FROM documents) LOOP
HTP.anchor('document_api.download?file=' || cur_rec.name, 'document_api.download?file=' || cur_rec.name);
HTP.br;
END LOOP;
HTP.bodyclose;
HTP.htmlclose;
END;
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
PROCEDURE download IS
-- ----------------------------------------------------------------------------
l_filename VARCHAR2(255);
BEGIN
l_filename := SUBSTR(OWA_UTIL.get_cgi_env('PATH_INFO'), 2);
WPG_DOCLOAD.download_file(l_filename);
EXCEPTION
WHEN OTHERS THEN
HTP.htmlopen;
HTP.headopen;
HTP.title('File Downloaded');
HTP.headclose;
HTP.bodyopen;
HTP.header(1, 'Download Status');
HTP.print('Download of ' || l_filename || ' failed.');
HTP.print(SQLERRM);
HTP.bodyclose;
HTP.htmlclose;
END;
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
PROCEDURE download (file IN VARCHAR2) AS
-- ----------------------------------------------------------------------------
l_blob_content documents.blob_content%TYPE;
l_mime_type documents.mime_type%TYPE;
BEGIN
SELECT blob_content,
mime_type
INTO l_blob_content,
l_mime_type
FROM documents
WHERE name = file;
OWA_UTIL.mime_header(l_mime_type, FALSE);
HTP.p('Content-Length: ' || DBMS_LOB.getlength(l_blob_content));
OWA_UTIL.http_header_close;
WPG_DOCLOAD.download_file(l_blob_content);
EXCEPTION
WHEN OTHERS THEN
HTP.htmlopen;
HTP.headopen;
HTP.title('File Downloaded');
HTP.headclose;
HTP.bodyopen;
HTP.header(1, 'Download Status');
HTP.print(SQLERRM);
HTP.bodyclose;
HTP.htmlclose;
END;
-- ----------------------------------------------------------------------------
END;
/
SHOW ERRORSOnce this code is loaded an upload can be performed by adjusting the form action in the upload.html file.
The upload procedure does not really upload the file as this is done automatically by the DAD. It simply allows you to perform additional processing if necessary. Oracle prefixes a the file name with a generated folder name to reduce the posibility of name conflicts. In this example the files are renamed to their original names.
In addition the upload code produces two types of links to test the download code. The first set of links use the "document access path" and the "document access procedure" defined in the DAD. These allow access to the files using a regular URL, masking the procedure call which actually returns the file. Using the document access path in the URL causes the DAD to call the document access procedure. The requested file name must be retrieved from the URL and used in the WPG_DOCLOAD.download_file procedure call.
The second set of links call an alternative download procedure directly. In this case file name must be specified as a parameter.
For further information see the DAD documentation loaded on your server. This is typically accessed using a URL like:
http://yourserver:7777/pls/simpledad/admin_/help/feature.htm#1004906
来自:http://www.oracle-base.com/articles/9i/FileUploadDownloadProcedures9i.php
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/48770/viewspace-179607/ ,如需转载,请注明出处,否则将追究法律责任。