# This example demonstrates you how "file upload": sending # a file to the server thru a html form. # allow file upload # uploadfilesize defines the maximum file size upload # in this case 100 bytes web::config uploadfilesize 100 # utility command to handle an HTML page proc page {title code} { web::putx {{web::put $title}

{web::put $title}

} uplevel $code web::put "\n" } # utility command to handle an html form proc form {page code} { web::put "
" uplevel $code web::put "
" } proc showForm {error} { # generate a page with Title "File upload example" page "File upload example" { # generate a form with action "submit" form "submit" { web::putx {{ # if "error" flag is set, show the red error message asking for input if {$error == 1} { web::put "If you'd like to upload a file,\n you have to insert the path and file name
" } } File:    } } } } # validator: # # make sure we have a name that is more than 1. proc checkFormData {} { # string lenght gets the lenght # "lindex [...] 1" is getting the first line from the list in the variable if { [string length [lindex [web::formvar upload] 1]] < 1} { # return error code return 1 } # looks good: no error return 0 } proc showConfirmationPage {} { # gets return value from list upload set localname [lindex [web::formvar upload] 0] set remotename [lindex [web::formvar upload] 1] set NumBytesTruncated [lindex [web::formvar upload] 2] # open pipe for reading uploaded file set fh [open $localname r] set chunk [read $fh 10] close $fh page "File upload example" { web::putx { We have received your file. Thank you.

Technical information:

File-path:{web::put [web::htmlify $localname]}
Localpath:{web::put [web::htmlify $remotename]}
We have configured websh3 to allow a maximum file size of 100 bytes. Therefore we have truncated the received file by {web::put [web::htmlify $NumBytesTruncated]} bytes.
Start of content:{web::put [web::htmlify $chunk]}
upload another file } } } # register the "default" command # # See confirmation form example. web::command default { showForm 0 } # register command "submit" # # This is the "action" of our form. See confirmation form example. web::command submit { if { [set res [checkFormData]] == 0 } { showConfirmationPage } else { showForm $res } } #see dispatch example web::dispatch