66 lines · 2.5 KB · Text
RawDownload
Example 2: Form-based file upload in HTML----------------------------------------- To upload a file from the client (browser) to a WWW server, you needto write the following components:         (A) An HTML page containing a FORM        (B) A CGI program to receive the file, process it, and respond  (A) You need an HTML page containing a FORM.  The FORM must have         - METHOD="POST" and ENCTYPE="multipart/form-data"          - an ACTION equalling the URL of a CGI program        - an INPUT field of TYPE=FILE        - one or more INPUT fields with TYPE=SUBMIT        - optionally other fields (INPUT, SELECT, ...) See file `upload.html' for an example HTML form.  With that form, youcan select a local file (on the system running the browser) and choosewhether you want to count the number of characters in the file, countthe number of lines in the file, or sort the file. Note that with upload.html, the chosen file is transmitted as a partcalled "filecontents", and the selected menu option is transmitted asanother part called "action".  (B) You need a CGI program See file `cgiex2.sml' for an example CGI program.  That program willhandle requests sent from the above-mentioned example form.  Withinthe example CGI program, the contents of the uploaded file isretrieved as        Mosmlcgi.part_data (valOf (Mosmlcgi.cgi_part "filecontents"))and the chosen select menu option is retrieved as        Mosmlcgi.part_data (valOf (Mosmlcgi.cgi_part "action"))Morover, the name of the uploaded file can usually be retrieved as        Mosmlcgi.part_field_string (valOf (Mosmlcgi.cgi_part "filecontents"))                                    "filename"  INSTRUCTIONS FOR USE (1) Edit the FORM in file `upload.html' so that ACTION refers to acgi-bin directory on your local webserver. (2) Compile the CGI program:    mosmlc -c cgiex2.sml   mosmlc -o cgiex2 cgiex2.uo (3) Install the compiled CGI program on your local webserver:    cat `which camlrunm` cgiex2 > /var/lib/httpd/cgi-bin/sestoft/cgiex2   chmod a+x /var/lib/httpd/cgi-bin/sestoft/cgiex2 The above assumes that your webserver lives in /var/lib/httpd/ so thatyour CGI program directory is /var/lib/httpd/cgi-bin/sestoft/, andthat you are allowed to install CGI programs on your webserver.Change the path as appropriate.  (Note that the runtime system issimply prepended to the ML bytecode, so that the webserver need notlook for it.) The included Makefile automates steps (2) and (3) above. (4) Open the HTML file `upload.html' with your webbrowser, select a file and an action, and click on `Send'.