Skip Headers
Oracle® Database Express Edition 2 Day Plus Application Express Developer Guide
Release 2.1

Part Number B25310-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

8 How to Upload and Download Files in an Application

Oracle Application Express applications may include the ability to upload and download files stored in the database. This tutorial illustrates how to create a form and report with links for file upload and download, how to create and populate a table to store additional attributes about the documents, and finally how to create the mechanism to download the document in your custom table.

This section contains the following topics:

Creating an Application

First, create a new application using the Create Application Wizard with the assumption you will include an upload form on page 2.

To create an application using the Create Application Wizard:

  1. On the Database Home Page, click the Application Builder icon.

    The Application Builder home page appears.

  2. Click Create.

  3. Select Create Application and click Next.

  4. Specify the page name.

    1. For Name, enter Download App.

    2. For Application, accept the default.

    3. For Create Application, select From scratch.

    4. For Schema, accept the default.

    5. Click Next.

  5. Add a blank page:

    1. Under Select Page Type, select Blank and click Add Page as shown in Figure 8-1.

      The new page appears in the list at the top of the page.

    2. Click Next.

  6. For Tabs, accept the default, One Level of Tabs, and click Next.

  7. For Copy Shared Components from Another Application, accept the default, No, and click Next.

  8. For Attributes, accept the defaults for Authentication Scheme, Language, and User Language Preference Derived From and click Next.

  9. For User Interface, select Theme 2 and click Next.

  10. Review your selections and click Create.

    The Application home page appears.

Creating an Upload Form

Once you create an application, the next step is to create a form to upload documents. In the following exercise you create a form in an HTML region that contains a file upload item and a button. The button submits the page and returns the user to the same page.

Topics in this section include:

Create an HTML Region

First, you need to create a container to hold the form. In Application Builder, this container is called a region.

To create an HTML region:

  1. Navigate to the Page Definition for page 1 by clicking Page 1.

  2. Under Regions, click the Create icon as shown in Figure 8-2.

  3. For Region, select HTML and click Next.

  4. Select a type of HTML region container. Select HTML again and click Next.

  5. For Display Attributes:

    1. In Title, enter Submit File.

    2. Accept the remaining defaults and click Next.

  6. Accept the remaining defaults and click Create Region.

    The Page Definition appears.

Create an Upload Item

Next, you need to create a text field or item. In Application Builder, an item is part of an HTML form. An item can be a text field, text area, password, select list, check box, and so on.

To create a file upload item:

  1. On the Page Definition for page 1, locate the heading Items.

  2. Under Items, click the Create icon.

  3. For Item Type, select File Browse and click Next.

  4. For Display Position and Name:

    1. For Item Name, enter P1_FILE_NAME.

    2. For Sequence, accept the default.

    3. For Region, select Submit File.

    4. Click Next.

  5. Accept the remaining defaults and click Next.

  6. Click Create Item.

    The Page Definition appears.

Create a Button

Next, you need to create a button to submit the file.

To create a button:

  1. Under Buttons, click the Create icon.

  2. For Button Region, select Submit File (1) 1 and click Next.

  3. For Button Position, select Create a button in a region position and click Next.

  4. On Button Attributes:

    1. For Button Name, enter Submit.

    2. Accept the remaining defaults.

    3. Click Next.

  5. For Button Template, accept the default and click Next.

  6. On Display Properties, accept the defaults and click Next.

  7. On Branching:

    1. In Branch to Page, select Page 1.

    2. Click Create Button.

  8. Run the page by clicking the Run Page icon as shown in Figure 8-3.

  9. When prompted for a user name and password:

    1. For User Name, enter the name of your database user account.

    2. For Password, enter the password for your database user account.

    3. Click Login.

When you run the page, it should look similar Figure 8-4.

Figure 8-4 Submit File Form

Description of Figure 8-4 follows
Description of "Figure 8-4 Submit File Form"

Creating a Report with Download Links

Once you create the form to upload documents, the next step is to create a report on the document table that contains links to download documents. When you use the file upload item type, the files you upload are stored in a table called wwv_flow_file_objects$. Every database user account has access to this table through a view called HTMLDB_APPLICATION_FILES.

Topics in this section include:

Create a Report on HTMLDB_APPLICATION_FILES

To create a report on HTMLDB_APPLICATION_FILES:

  1. Navigate to the Page Definition for page 1. Click Edit Page 1 on the Developer toolbar.

  2. Under Regions, click the Create icon.

  3. For Region, select Report and click Next.

  4. Specify the type of report. For Report Implementation, select SQL Report and click Next.

  5. For Display Attributes:

    1. In Title, enter Uploaded Files.

    2. Accept the remaining defaults and click Next.

  6. For Source, enter the following SQL query:

    SELECT id,name FROM HTMLDB_APPLICATION_FILES
    
    
  7. Click Create Region.

  8. Run the page by clicking the Run Page icon.

As shown in Figure 8-5, the report you just created shows all documents that have been uploaded.

Figure 8-5 Uploaded Files Report

Description of Figure 8-5 follows
Description of "Figure 8-5 Uploaded Files Report"

Add Link to Download Documents

Next, you need to provide a link to download each document.

To provide a link to download the documents in the report:

  1. Navigate to the Page Definition for page 1. Click Edit Page 1 on the Developer toolbar.

  2. Under Regions, click Report adjacent to Uploaded Files as shown in Figure 8-6.

    The Report Attributes page appears. You can add a link to the ID column by editing Column Attributes.

  3. Under Column Attributes, click the Edit icon next to the ID column.

  4. Scroll down to Column Link.

  5. Under Column Link:

    1. In the Link Text field, enter:

      download
      
      
    2. From Target, select URL.

    3. In the URL field, enter the following:

      p?n=#ID#
      
      

      #ID# passes the value contained in the column where ID is the column alias.

  6. Scroll back to the top of the page and click Apply Changes.

    The Page Definition appears.

  7. Run the page by clicking the Run Page icon.

    When you run the page, it should look similar to Figure 8-7.

    Figure 8-7 Uploaded Files Report with Download Links

    Description of Figure 8-7 follows
    Description of "Figure 8-7 Uploaded Files Report with Download Links"

  8. Click Edit Page 1 on the Developer toolbar to return to the Page Definition.

  9. Return to the Database Home Page. Click the Home breadcrumb link at the top of the page as shown in Figure 8-8.

    Figure 8-8 Breadcrumb Menu

    Description of Figure 8-8 follows
    Description of "Figure 8-8 Breadcrumb Menu"

Storing Additional Attributes About the Document

Next, you create another table to store additional information about the documents that are uploaded. In this exercise you:

Topics in this section include:

Create a Table to Store Document Attributes

First, you create a table in SQL Commands.

To create the table to store additional information about uploaded files:

  1. On the Database Home Page, click the SQL icon.

  2. Click SQL Commands.

  3. In the SQL Commands, enter:

    CREATE TABLE file_subjects(name  VARCHAR2(4000), subject VARCHAR2(4000) );
    
    
  4. Click Run.

    The message Table created appears.

  5. Click the Home breadcrumb link.

    The Database Home Page appears.

Create an Item to Capture the Document Subject

To create an item to capture the subject of the document:

  1. Navigate to the Page Definition for page 1:

    1. On the Database Home Page, click the Application Builder icon.

      The Application Builder home page appears.

    2. Select Download App.

      The Application home page appears.

    3. Select Page 1.

    The Page Definition for Page 1 appears.

  2. Under Items, click the Create icon.

  3. For Item Type, select Text and click Next.

  4. For Text Control Display Type, select Text Field and click Next.

  5. For Display Position and Name:

    1. For Item Name, enter P1_SUBJECT.

    2. For Sequence, accept the default.

    3. From Region, select Uploaded Files.

    4. Click Next.

  6. For Item Attributes:

    1. In the Label field, enter Subject.

    2. Accept the remaining defaults.

    3. Click Next.

  7. Click Create Item.

Create a Process to Insert Information

Next, you need to create a process to insert the subject information into the new table.

To create a process:

  1. Under Page Processing, Processes, click the Create icon as shown in Figure 8-9.

  2. For Process Type, select PL/SQL and click Next.

  3. For Process Attributes:

    1. For Name, enter Insert.

    2. For Sequence, accept the default.

    3. From Point, select On Submit - After Computations and Validations.

    4. Click Next.

  4. In Enter PL/SQL Page Process, enter the following:

    INSERT INTO file_subjects(name, subject) VALUES(:P1_FILE_NAME,:P1_SUBJECT);
    
    
  5. Click Next.

  6. For Messages:

    1. In Success Message, enter:

      Subject inserted
      
      
    2. In Failure Message enter:

      Error inserting subject
      
      
    3. Click Next.

  7. For Process Conditions:

    1. From When Button Pressed, select SUBMIT.

    2. Accept the remaining defaults and click Create Process.

Showing Additional Attributes in the Report Region

Finally, you need to alter the SQL Report region to join it to the additional attributes table. To accomplish this, you edit the Region Source attribute on the Region Definition page.

To edit the Region Source:

  1. Under Regions, click Uploaded Files as shown in Figure 8-10.

    Figure 8-10 Uploaded Files Link

    Description of Figure 8-10 follows
    Description of "Figure 8-10 Uploaded Files Link"

    The Region Definition appears.

  2. Scroll down to Source.

  3. Replace the Region Source with the following:

    SELECT w.id,w.name,s.subject  
    FROM HTMLDB_APPLICATION_FILES w,file_subjects s
    WHERE w.name = s.name
    
    
  4. Scroll up to the top of the page and click Apply Changes.

  5. Run the page by clicking the Run Page icon.

    As shown in Figure 8-11, the Uploaded Files report now contains a Subject column.

    Figure 8-11 Uploaded Files Report with Subject Column

    Description of Figure 8-11 follows
    Description of "Figure 8-11 Uploaded Files Report with Subject Column"

    If your Uploaded Files report does not initially contain all three columns, try uploading a file and clicking the Submit button.

  6. Return to the Page Definition by clicking Edit Page 1 on the Developer toolbar.

  7. Return to the Database Home Page. Click the Home breadcrumb link at the top of the page.

Store the Document in a Custom Table

In certain cases, you may want to store uploaded documents in a table owned by your schema. For example, if you want to create an Oracle Text index on uploaded documents, you need to store the documents in a custom table.

To store documents in your custom table:

To add a BLOB column to the file_subjects table:

  1. On to the Database Home Page, click the SQL icon.

  2. Click SQL Commands.

    SQL Commands appears.

  3. Enter the following SQL statement:

    ALTER TABLE file_subjects ADD(id number,blob_content BLOB,mime_type varchar2(4000) );
    
    
  4. Click Run.

    The message Table Altered appears.

  5. Return to the Database Home Page. Click the Home breadcrumb link at the top of the page.

To alter the process to insert documents into the file_subjects table:

  1. On the Database Home Page, click the Application Builder icon.

  2. Select Download App.

  3. Select Page 1.

  4. Under Processes, select Insert.

  5. Scroll down to Source.

  6. Under Source, replace the process with the following:

    IF ( :P1_FILE_NAME is not null ) THEN 
         INSERT INTO file_subjects(id,NAME, SUBJECT, BLOB_CONTENT, MIME_TYPE) 
          SELECT ID,:P1_FILE_NAME,:P1_SUBJECT,blob_content,mime_type
                FROM HTMLDB_APPLICATION_FILES WHERE name = :P1_FILE_NAME;
                WHERE name = :P1_FILE_NAME;
       DELETE from HTMLDB_APPLICATION_FILES WHERE name = :P1_FILE_NAME;
      END IF;
    
    
  7. Scroll up to the top of the page and click Apply Changes.

  8. Return to the Database Home Page. Click the Home breadcrumb link at the top of the page.

Downloading Documents from the Custom Table

Now that documents are being stored in a custom table, you need to provide a way to download them. You do this by creating a procedure and granting execute on that procedure to the pseudo user HTMLDB_PUBLIC_USER.

To accomplish this you need to change:

To create a procedure to download documents from the file_subjects table and grant execute to public:

  1. On the Database Home Page, click the SQL icon.

  2. Click SQL Commands.

  3. Enter the following SQL statement:

    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 MIME_TYPE, BLOB_CONTENT, name,DBMS_LOB.GETLENGTH(blob_content)
                    INTO v_mime,lob_loc,v_file_name,v_length
                    FROM file_subjects
                    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="'||substr(v_file_name,instr(v_file_name,'/')+1)|| '"');
                    -- close the headers            
                    owa_util.http_header_close;
                    -- download the BLOB
                    wpg_docload.download_file( Lob_loc );
    end download_my_file;
    /
    
    
  4. Click Run.

    The message Procedure Created appears. Run another SQL statement.

  5. Click SQL breadcrumb link and then SQL Commands.

    SQL Commands appears.

  6. Enter the following SQL statement:

    GRANT EXECUTE ON download_my_file TO PUBLIC/
    
    
  7. Click Run.

    The message Statement processed appears.

  8. Return to the Database Home Page. Click the Home breadcrumb link at the top of the page.

To change the SQL report region to no longer join with the HTMLDB_APPLICATION_FILES view:

  1. Navigate to the Page Definition of page 1:

    1. On the Database Home Page, click the Application Builder icon.

      The Application Builder home page appears.

    2. Select Download App.

      The Application home page appears.

    3. Select Page 1.

  2. Under Regions, select Uploaded Files.

  3. Scroll down to Source.

  4. Replace the Region Source with the following:

    SELECT s.id,s.name,s.subject FROM file_subjects s
    
    
  5. Scroll up to the top of the page and click Apply Changes.

    The Page Definition appears.

To change the download link to use the new download procedure:

  1. Under Regions, click Report adjacent to Uploaded Files.

  2. Next to the ID column, click the Edit icon.

  3. Scroll down to the Column Link.

  4. Replace the existing URL with the following:

    #OWNER#.download_my_file?p_file=#ID#
    
    

    In this URL:

    • #OWNER# is the parsing schema of the current application.

    • download_my_file is the new procedure you just created.

    • You are passing in the value of the column ID to the parameter p_file.

  5. Scroll up to the top of the page and click Apply Changes.