XForms/Upload

Motivation
The upload element allows you to select a file from the file system using your operating system's "Browse" user interface. This file can then be transferred to the file server that the web form resides.

Screen Image
Here is a screen image of the Firefox XForms extension upload control after the C:\tmp\Foo.xml file was selected:



Sample Program
This sample program has a single instance variable called "File". At the end of the select this variable is set to the path-name of the file you just selected.

   file:///C:/tmp/*.xml text/xml   File:  

Discussion
At the time of this writing, there is little documentation on how the upload control works. The screen shot above was taken from the Firefox XForms 0.8 running on Firefox 3 implementation. Note that the file name appears twice in the control itself. This may not be the future behavior. This may be that the filename and mediatype text inside the control are being displayed. There is no documentation yet on how to disable the clear trigger.

There is not yet documentation on how to upload instance data into the model using the upload control. Right now instance data must be hard-coded into the  attribute of an instance.

The following loads both an image and a URI.

    <xf:label>Upload Photo:</xf:label> </xf:upload> <xf:upload ref="uri"> <xf:label>Upload File:</xf:label> <xf:filename>file:///C:/tmp/*.xml</xf:filename> <xf:mediatype>text/xml</xf:mediatype> </xf:upload> <xf:output ref="image"> <xf:label>image: </xf:label> </xf:output> <xf:output ref="uri"> <xf:label>uri: </xf:label> </xf:output> <xf:submit submission="save"> <xf:label>Save</xf:label> </xf:submit>

Uploading Binary Files
Any file type such as an image or XML file can be converted to a 64bit encoded file, stored in the instance and then transmitted to the server inside of a POST. On the server the file can be converted back to a string or binary.

Sample Echo With Binary to String Unencoding
The following is a simple echo script written in XQuery that echos back the binary file in XML. It is an XQuery script that uses a binary-to-string conversion function. This takes a file of type xs:base64binary and returns a string representation of the file. The data to be returned is in the element with the element name "xml-base64".

Sample Form
</xf:instance> <xf:submission id='post-to-echo' action='echo-base64-binary.xq' replace="all" method='post'/> </xf:model> <xf:upload ref="xml-base64"> <xf:label>Upload XML File:</xf:label> </xf:upload> <xf:output ref="xml-base64"> <xf:label>XML file encoded in base64binary: </xf:label> </xf:output> <xf:submit submission="post-to-echo"> <xf:label>Post to Echo</xf:label> </xf:submit>

Warnings
The attribute data type assignment  must be associated with the  variable used to store the file path name.