Another useful APyaX feature allows form data to be uploaded to the server and passed to a python function. Forms may include text fields and files.
The APyaX generator searches the input (X)HTML document for forms with an onsubmit function call of the form:
The example (X)HTML illustrates how a <form> is set up to integrate with APyaX.
The function uploadForm below shows how the form object can be inspected from Python.
def uploadForm(apyax,form): pstr = "uploadit:" pstr += " strings: " stringkeys = form.getStringKeys() for stringkey in stringkeys: s = form.getString(stringkey) pstr += s pstr += " " pstr += " files: " filekeys = form.getFileKeys() for filekey in filekeys: (filename,file,type) = form.getFile(filekey) file.seek(0,2) filesize = file.tell() pstr += "(" + filename + "," + str(type) + "," + str(filesize)+" bytes) " return "OK (" + pstr +")"The example APyaX python script uploadit.py
The apyax_form object that APyaX passes as the second argument to the form upload function provides access to the form's contents via the following methods:
|Server side object to provide access to the contents of an uploaded form|
Get information on an uploaded file associated with a form
Returns a tuple (<filename>,<file>,<type>) for a file with a matching key value <key> (or None if no match found) where: |
<filename> is the name of the file
<file> is a file-like object
<type> is a string containing the mime-type
|name||the string key which identifies the file|
|tuple describing file|
|get keys associated with a form's uploaded files|
|a list of the keys (as string values) for files included in the form|
|Get information on a non-file parameter associated with a form|
|name||the string key which identifies the value|
|the value for the given key value <key>, or None if no matching key is found.|
|get keys associated with a form's non-file values|
|a list of the keys (as string values) for non-file objects in the form.|