|
File Uploads
| [Previous] [Main] [Next] |
|
| |
| Some browsers support HTTP file upload capability via the <INPUT TYPE="FILE"> tag. This feature is especially useful because it allows users to transmit images, documents, and other files directly to your html++ application from a form without the use of FTP. Note that this tag is only supported by NetScape Navigator 2.0 and later, and Microsoft Internet Explorer 3.02 and later.
| |
|
| |
| The <INPUT TYPE="FILE"> tag requires special processing on the server side, known as multipart mime decoding. This type of decoding is not normally performed by the HTTP server software, so it is up to developers to provide their own support if the HTTP file upload feature is to be used. Since decoding multipart mime data is fairly difficult, the use of HTTP file upload capabilities is not widely used by web site designers.
| |
|
| |
| html++ makes decoding multipart mime data easy, and automatically handles everything you need to add HTTP file upload features to forms.
| |
|
| |
| The htmlInputFile class encapsulates the <INPUT TYPE="FILE"></INPUT> tags. This class implements form-based file upload capabilities as described in RFC1867. It is used to create a text input control for a filename on a form, that includes a "Browse..." button next to the text input control, allowing users to select and upload a file from their system. htmlInputFile is derived from htmlInputText, so all the same methods from that class can be used.
| |
|
| |
| When the form is submitted, the content of the specified file is sent to the server along with the other form data.
| |
|
| |
| The HTML specification requires that forms containing file upload controls use the encoding type "multipart/form-data". Unless the htmlForm::Encode() method is called manually, the htmlForm class will automatically set the correct encoding type whenever an htmlInputFile object is added, relieving you of that concern.
| |
|
| |
| When receiving uploaded files from forms, the htmlCgi class will automatically capture and save the file contents in a temporary file. In such cases, the name of the form field will not appear in the CGI name/value list. Instead, htmlCgi adds two name/value pairs for each file element received.The names of these values are based on the form field name from which they originated, with the addition of a dot followed the word "local" or "remote". They are accessible via the htmlCgi::Value() method.
| |
|
| |
| For example, if the field name representing the file upload button on the form were named "file", htmlCgi would generate name/value pairs using the following two names:
| |
| file.local
| |
| file.remote
| |
|
| |
| The value associated with the ".local" suffix represents the name of the local temporary file used to store the received file contents. The value associated with the ".remote" suffix represents the remote (on the browser) name of the file that the user selected on their system.
| |
|
| |
| It is the responsibility of the developer to use or delete temporary files after they are received.
| |
|
| |
|
| |
|
| |
| // Program 1: create the data input form
| |
| #include <stdio.h>
| |
| #include <stdlib.h>
| |
| #include <dcmicro/htmlpp/htmlpp.h>
| |
|
| |
| int main( void )
| |
| {
| |
| htmlCgi server ;
| |
| htmlPage page( "html++ example application" ) ;
| |
|
| |
| // Create a simple form containing a text field
| |
| // and a file input field.
| |
| htmlForm form( "/cgi-bin/formdemo2" ) ;
| |
|
| |
| // Add some informative text in a bigger font
| |
| form << ( htmlFont("",+1)
| |
| << "The contents of the file you select "
| |
| "will be uploaded to the server." )
| |
| << htmlParagraph() ;
| |
|
| |
| // Add the file input control
| |
| form << "File name: "
| |
| << htmlInputFile( "file" )
| |
| << htmlParagraph() ;
| |
|
| |
| // Add a regular text control on the next line
| |
| form << "Description: "
| |
| << htmlInputText( "desc", 30, 60 )
| |
| << htmlParagraph()
| |
| << htmlInputSubmit( "Continue" ) ;
| |
|
| |
| // Add the newly created form to the page
| |
| page << form ;
| |
|
| |
| // Output the completed page
| |
| server << page ;
| |
| return 0 ;
| |
| }
| |
|
| |
|
| |
|
| |
| // program 2: Process the form data
| |
| #include <stdio.h>
| |
| #include <stdlib.h>
| |
| #include <dcmicro/htmlpp/htmlpp.h>
| |
|
| |
| int main( void )
| |
| {
| |
| htmlPage page( "html++ formdemo2 application" ) ;
| |
|
| |
| // Construct an htmlCgi object to receive
| |
| // the CGI form data.
| |
| htmlCgi server( cin ) ;
| |
|
| |
| // Output all CGI data from the form
| |
| String name ;
| |
| String value ;
| |
| Boolean good_data = server.Enumerate( name, value, TRUE ) ;
| |
| while ( good_data )
| |
| {
| |
| page << htmlBold( name )
| |
| << " : "
| |
| << value
| |
| << htmlBreak() ;
| |
|
| |
| // If a file was uploaded, delete it. Normally,
| |
| // one would probably copy the file to a different
| |
| // location for an application-specific purpose.
| |
| if ( name.Contains( ".local" ) )
| |
| unlink( (char *) value ) ;
| |
|
| |
| // loop for next form field
| |
| good_data = server.Enumerate( name, value ) ;
| |
| }
| |
|
| |
| server << page ;
| |
| return 0 ;
| |
| }
| |
|
| |
|
| |
| Content-Type: text/html
| |
| <HTML>
| |
| <HEAD>
| |
| <TITLE>html++ example application</TITLE>
| |
| </HEAD>
| |
| <BODY>
| |
| <FORM ACTION="/cgi-bin/formdemo2" METHOD="POST" ENCTYPE="multipart/form-data">
| |
| <FONT SIZE="+1">The contents of the file you select will be uploaded to the server.</FONT><P></P>
| |
| File name: <INPUT TYPE="FILE" NAME="file"><P></P>
| |
| Description: <INPUT TYPE="TEXT" NAME="desc" SIZE="30" MAXLENGTH="60"><P></P>
| |
| <INPUT TYPE="SUBMIT" VALUE="Continue">
| |
| </FORM>
| |
| </BODY>
| |
| </HTML>
| |
|
| |
|
| |
|
| |
| The window displayed when the Browse button is clicked.
|
|
|
|
|
|
| |
| Content-Type: text/html
| |
| <HTML>
| |
| <HEAD>
| |
| <TITLE>html++ formdemo2 application</TITLE>
| |
| </HEAD>
| |
| <BODY>
| |
| <B>file.local</B> : /var/tmp/tmp.0.028753<BR>
| |
| <B>file.remote</B> : c:\acropolis.txt<BR>
| |
| <B>desc</B> : This is the file description.<BR>
| |
| </BODY>
| |
| </HTML>
| |
|
| |
|
| |
