|
Abstract: Form-based file uploads is the ability for clients to send files from the client machine to the server machine using an HTML input field in an HTML form.
What is it?
Form-based file uploads enables users to select a file on their machine that will be sent to the server. This is done by using an HTML form that contains one or more file-selection input fields. A user would fill out the form, which would include selecting a file, and then submit the form. The form data, including the file, is sent to the form processing application specified in the ACTION attribute.
Why would I want to use it?
There are a lot of uses for this support. One example is a job application form in which the applicant can send a resume. Another example would be an insurance form where the insurance agent can upload insurance forms.
How do I use it?
To enable Net.Data to handle form-based file uploads, you must specify the DTW_UPLOAD_DIR configuration variable in the Net.Data INI file in which files will be saved. Here is an example:
DTW_UPLOAD_DIR /Resume
NOTE: If DTW_UPLOAD_DIR is not specified in the Net.Data INI file, files that are uploaded are discarded by Net.Data.
Now you need to create a form that will allow users to upload a file. Take a look at the form below:
Listing 1. The HTML form upload.htm
To create a form that allows users to upload files, the ENCTYPE must be specified with a value of multipart/form-data and one or more input fields must be specified with a TYPE of FILE. When the form is displayed on the browser, it will look something like the following:
Users can either type in the file name in the input field or use the browse button to select a file using a dialog box presented by the browser.
When the user submits the form, the HTTP server will invoke Net.Data ( nd in the ACTION maps to Net.Data). When Net.Data gets called, it will store the file in the directory specified by the DTW_UPLOAD_DIR configuration variable. Net.Data does not perform any conversion on the uploaded files, it is treated as binary data. However, Net.Data does attempt to ensure that the file is tagged with the correct codepage by mapping the character set associated with file, if specified, to the corresponding codepage. Character sets are specified for files that are textual in nature. The uploaded files are stored in the directory specified in DTW_UPLOAD_DIR and are given a unique name, determined using the following rules:
MacroFileName + '.' + FormVarName + '.' +
UniqueIdentifier + '.' + FileName
Where MacroFileName is the name of the macro handling the request; FormVarName is the name of the variable used to identify the file in the form; UniqueIdentifier is a string used to ensure uniqueness; and FileName is the name of the file that is being uploaded (only the name is used, not the full path).
On the System i, the unique identifier used in the generated file name has the following format:
YYYYMMDDHHMMSSCCC + '-' + PID + '-' + TID
Where YYYY, MM, DD is the year, month and day; HH, MM, SS, CCC is hours, minutes, seconds, and milliseconds; PID is the process ID, and TID is the thread ID.
After Net.Data processes the form data, it will process the macro. The macro that handles the form above is given below:
Listing 2. The Net.Data macro upload.dtw
%HTML(jobApplication) {
The name of the job applicant is "$(name)".
The job applicant's resume is
located here: "$(resume)".
%}
As you can see from the above macro, the file input field variable resume will contain the absolute (full) path to the file name. If the form was submitted and the macro was processed, you would see the following on the browser:
The name of the job applicant is "firstName lastName".
The job applicant's resume is located here:
"/Resume/upload.dtw.resume.20000313112341275-245-1.file.txt"
You may want to move the file and rename it to something more meaningful. You can do so using the MOV CL command.
NOTE: IE browser users should upgrade to latest version (5.1) or later version of the browser since testing has determined that on some older versions the browser would sometimes send mal-formed form data when certain characters are submitted.
Availability
V4R1 and subsequent releases. Ensure you have the latest Net.Data PTF.
|