PATH |
Providing for File Uploads
Discussion
Most of today's Web browsers permit the attachment of files to a form submission, useful for such tasks as adding a Microsoft Word document to a resume database or storing a photograph of an employee being added to a company directory. WebObjects 4.0 adds the WOFileUpload component to support RFC 1867, which defines a method of submitting form information using "multipart MIME." Previous versions of WebObjects required additional support that is available from the NeXTanswers on-line support system (see NeXTanswer #2505).
The following browsers work with the WebObjects WOFileUpload component:
- Netscape Navigator 2.x and greater
- Microsoft Internet Explorer version 4.x and greater
- Microsoft Internet Explorer version 3.01, if the "RFC 1867" add-on is installed
- Omnigroup OmniWeb version 2.x and greater
- Other browsers that support RFC 1867
To add file-upload support, you must have the WOFileUpload component in a WOForm. You must also add an additional binding to the WOForm to specify that the browser should use multipart MIME encoding to submit the form information (which includes the attached files).
The following steps illustrate how to add file upload support to your application. The example uses the Movies EOModel and a Wizard-generated application. You will add the capability to upload the image of the talent to the application. The image is the photo attribute of the TalentPhoto object. The TalentPhoto object is referenced by the photo relationship of the currently selected talent .
To prepare the example, perform the following steps:
- Create a new "WebObjects Application" project, selecting "Wizard" for assistance and "Java" as the primary language.
- Choose the "Existing Model" option and select the Movies.eomodeld file.
- Select the "Talent" Entity.
- Choose "Selected Record" and "Paginated" for the layout.
- Include the "firstName" and "lastName" attributes to display, and the "lastName" for the hyperlink.
To add file upload support, perform the following steps:
Open the "Main.wo" component in WebObjectsBuilder.
- Inspect the WOForm at the bottom of the page.
- Add an attribute named "enctype" and set its value to "multipart/form-data".
- Add a variable "filename" of type String to the component.
- Add a "Custom WebObject" component to the WOForm (available from the Dynamic elements toolbar or from the "Elements->WebObjects" menu).
- In the inspector for the component, enter "WOFileUpload" for the class name, and add two attributes, one labeled "filePath" and the other labeled "data".
- Create the following bindings:
- Build and run the example. You should see a text field and button where the WOFileUpload component was. You can click on the button to select a file. When the form is submitted, the attribute bound to "data" contain the contents of the file being uploaded.
In this example, you should upload "GIF" images that are photographs of people. When you click "Save to Database", the file data is committed to the database as a BLOB since the attribute is a NSData object.
See Also
- Displaying Images Stored in a Database
- RFC 1867 (available at: http://info.internet.isi.edu:80/in-notes/rfc/files/rfc1867.txt)
- WOFileUpload specification in the WebObjects Dynamic Element Specifications
Revision History
22 July 1998. Timothy Joransen. First Draft.
19 November 1998. Clif Liu. Second Draft.
© 1999 Apple Computer, Inc.