| <!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> |
| <HTML> |
| <HEAD> |
| <TITLE>upload</TITLE> |
| </HEAD> |
| <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#1F00FF" ALINK="#FF0000" VLINK="#9900DD"> |
| <A NAME="top"> |
| <A NAME="file1"> |
| <H1>upload.ws3</H1> |
| |
| <PRE> |
| <I><FONT COLOR="#B22222"># This example demonstrates you how "file upload": sending |
| </FONT></I><I><FONT COLOR="#B22222"># a file to the server thru a html form. |
| </FONT></I> |
| <I><FONT COLOR="#B22222"># allow file upload |
| </FONT></I><I><FONT COLOR="#B22222"># uploadfilesize defines the maximum file size upload |
| </FONT></I><I><FONT COLOR="#B22222"># in this case 100 bytes |
| </FONT></I>web::config uploadfilesize 100 |
| |
| <I><FONT COLOR="#B22222"># utility command to handle an HTML page |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">page</FONT></B> {<FONT COLOR="#B8860B">title code</FONT>} { |
| web::putx {<html><head><title>{web::put $<FONT COLOR="#B8860B">title</FONT>}</title></head> |
| <body bgcolor=<B><FONT COLOR="#BC8F8F">"#ffffff"</FONT></B> <B><FONT COLOR="#A020F0">text</FONT></B>=<B><FONT COLOR="#BC8F8F">"#000000"</FONT></B>> |
| <h3>{web::put $<FONT COLOR="#B8860B">title</FONT>}</h3> |
| } |
| <B><FONT COLOR="#A020F0">uplevel</FONT></B> $<FONT COLOR="#B8860B">code</FONT> |
| web::put <B><FONT COLOR="#BC8F8F">"</body></html>\n"</FONT></B> |
| } |
| |
| <I><FONT COLOR="#B22222"># utility command to handle an html form |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">form</FONT></B> {<FONT COLOR="#B8860B">page code</FONT>} { |
| web::put <B><FONT COLOR="#BC8F8F">"<form enctype=\"multipart/form-data\" method=\"post\" action=\"[web::cmdurl $page]\">"</FONT></B> |
| <B><FONT COLOR="#A020F0">uplevel</FONT></B> $<FONT COLOR="#B8860B">code</FONT> |
| web::put <B><FONT COLOR="#BC8F8F">"</form>"</FONT></B> |
| } |
| |
| <B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">showForm</FONT></B> {<FONT COLOR="#B8860B">error</FONT>} { |
| |
| <I><FONT COLOR="#B22222"># generate a page with Title "File upload example" |
| </FONT></I> page <B><FONT COLOR="#BC8F8F">"File upload example"</FONT></B> { |
| <I><FONT COLOR="#B22222"># generate a form with action "submit" |
| </FONT></I> form <B><FONT COLOR="#BC8F8F">"submit"</FONT></B> { |
| web::putx {{ |
| <I><FONT COLOR="#B22222"># if "error" flag is set, show the red error message asking for input |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> {$<FONT COLOR="#B8860B">error</FONT> == 1} { |
| web::put <B><FONT COLOR="#BC8F8F">"<font color=\"\#990000\">If you'd like to upload a file,\n you have to insert the path and file name <br></font>"</FONT></B> |
| } |
| } |
| <b>File:</b> <input type=<B><FONT COLOR="#BC8F8F">"file"</FONT></B> size=<B><FONT COLOR="#BC8F8F">"30"</FONT></B> name=<B><FONT COLOR="#BC8F8F">"upload"</FONT></B> value=<B><FONT COLOR="#BC8F8F">"{web::put [web::formvar upload]}"</FONT></B>> &nbsp;&nbsp; <input type=<B><FONT COLOR="#BC8F8F">"submit"</FONT></B> name=<B><FONT COLOR="#BC8F8F">"ok"</FONT></B> value=<B><FONT COLOR="#BC8F8F">"Send"</FONT></B>> |
| } |
| } |
| } |
| } |
| |
| <I><FONT COLOR="#B22222"># validator: |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># make sure we have a name that is more than 1. |
| </FONT></I><B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">checkFormData</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| <I><FONT COLOR="#B22222"># string lenght gets the lenght |
| </FONT></I><I><FONT COLOR="#B22222"># "lindex [...] 1" is getting the first line from the list in the variable |
| </FONT></I> <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">string</FONT></B> length [<B><FONT COLOR="#A020F0">lindex</FONT></B> [web::formvar upload] 1]] < 1} { |
| <I><FONT COLOR="#B22222"># return error code |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 1 |
| } |
| <I><FONT COLOR="#B22222"># looks good: no error |
| </FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B> 0 |
| } |
| |
| |
| <B><FONT COLOR="#A020F0">proc</FONT></B> <B><FONT COLOR="#0000FF">showConfirmationPage</FONT></B> {<FONT COLOR="#B8860B"></FONT>} { |
| |
| <I><FONT COLOR="#B22222"># gets return value from list upload |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> localname [<B><FONT COLOR="#A020F0">lindex</FONT></B> [web::formvar upload] 0] |
| <B><FONT COLOR="#A020F0">set</FONT></B> remotename [<B><FONT COLOR="#A020F0">lindex</FONT></B> [web::formvar upload] 1] |
| <B><FONT COLOR="#A020F0">set</FONT></B> NumBytesTruncated [<B><FONT COLOR="#A020F0">lindex</FONT></B> [web::formvar upload] 2] |
| |
| <I><FONT COLOR="#B22222"># open pipe for reading uploaded file |
| </FONT></I> <B><FONT COLOR="#A020F0">set</FONT></B> fh [<B><FONT COLOR="#A020F0">open</FONT></B> $<FONT COLOR="#B8860B">localname</FONT> r] |
| <B><FONT COLOR="#A020F0">set</FONT></B> chunk [<B><FONT COLOR="#A020F0">read</FONT></B> $<FONT COLOR="#B8860B">fh</FONT> 10] |
| close $<FONT COLOR="#B8860B">fh</FONT> |
| |
| page <B><FONT COLOR="#BC8F8F">"File upload example"</FONT></B> { |
| |
| web::putx { |
| <b>We have received your <B><FONT COLOR="#A020F0">file</FONT></B>. Thank you.<br><br> |
| Technical information:</b> |
| <br> |
| <table border=<B><FONT COLOR="#BC8F8F">"0"</FONT></B> width=<B><FONT COLOR="#BC8F8F">"300"</FONT></B>> |
| |
| <tr> |
| <td width=<B><FONT COLOR="#BC8F8F">"100"</FONT></B>> |
| File-path:</td><td width=<B><FONT COLOR="#BC8F8F">"200"</FONT></B>>{web::put [web::htmlify $<FONT COLOR="#B8860B">localname</FONT>]}</td></tr> |
| <tr> |
| <td width=<B><FONT COLOR="#BC8F8F">"100"</FONT></B>> |
| Localpath:</td><td width=<B><FONT COLOR="#BC8F8F">"200"</FONT></B>>{web::put [web::htmlify $<FONT COLOR="#B8860B">remotename</FONT>]}</td></tr> |
| <tr> |
| <td width=<B><FONT COLOR="#BC8F8F">"300"</FONT></B> colspan=<B><FONT COLOR="#BC8F8F">"2"</FONT></B>> |
| We have configured websh3 to allow a maximum <B><FONT COLOR="#A020F0">file</FONT></B> size of 100 bytes. Therefore |
| we have truncated the received <B><FONT COLOR="#A020F0">file</FONT></B> by {web::put [web::htmlify $<FONT COLOR="#B8860B">NumBytesTruncated</FONT>]} bytes. |
| </td></tr> |
| <tr> |
| <td width=<B><FONT COLOR="#BC8F8F">"100"</FONT></B>>Start of content:</td><td width=<B><FONT COLOR="#BC8F8F">"200"</FONT></B>>{web::put [web::htmlify $<FONT COLOR="#B8860B">chunk</FONT>]}</td></tr></table> |
| <a href=<B><FONT COLOR="#BC8F8F">"{web::put [web::cmdurl default]}"</FONT></B>>upload another <B><FONT COLOR="#A020F0">file</FONT></B></a> |
| } |
| |
| } |
| } |
| |
| <I><FONT COLOR="#B22222"># register the "default" command |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># See confirmation form example. |
| </FONT></I>web::command default { |
| showForm 0 |
| } |
| |
| <I><FONT COLOR="#B22222"># register command "submit" |
| </FONT></I><I><FONT COLOR="#B22222"># |
| </FONT></I><I><FONT COLOR="#B22222"># This is the "action" of our form. See confirmation form example. |
| </FONT></I> |
| web::command submit { |
| |
| <B><FONT COLOR="#A020F0">if</FONT></B> { [<B><FONT COLOR="#A020F0">set</FONT></B> res [checkFormData]] == 0 } { |
| showConfirmationPage |
| } else { |
| showForm $<FONT COLOR="#B8860B">res</FONT> |
| } |
| } |
| |
| <I><FONT COLOR="#B22222">#see dispatch example |
| </FONT></I>web::dispatch</PRE> |
| <HR> |
| <ADDRESS>Generated by <A HREF="http://www.iki.fi/~mtr/genscript/">GNU enscript 1.6.3</A>.</ADDRESS> |
| </BODY> |
| </HTML> |