<body>done</body>
</html>
};
</pre>
=File Uploads=
{{Mark|Introduced with Version 7.7:}}
By using the content type {{Code|multipart/form-data}}, uploaded files can be handled server-side using RestXQ. A standard file upload input element can be used to upload files. Suppose you have a simple form (the HTML5 {{Code|multiple}} attribute enables the upload of multiple files):
<pre class="brush:xml">
<form name="myForm" action="/upload" method="POST" enctype="multipart/form-data">
Your Name: <input type="text" name="user-name"/><br/>
<input type="file" name="files[]" multiple="multiple" />
</form>
</pre>
In XQuery, the names and values of the {{Code|input}} elements will be represented as a [[Map module|map]]. Uploaded files (i. e., inputs of type {{Code|file}}) result in a filename and the file content, which is why the value of file inputs will thus be represented in an additional map. The following example stores all uploaded files in a temporary directory and returns XML elements for all inputs:
<pre class="brush:xquery">
declare
%rest:POST("{$data}")
%rest:path("/upload")
function local:upload($data)
{
for $key in map:keys($data)
let $value := $data($key)
return if($val instance of map(*)) then (
(: handles uploaded files :)
for $name in map:keys($value)
let $content := $files($name)
let $size := count(convert:binary-to-bytes($content))
return (
file:write-binary(file:temp-dir() || $name, $content),
<file name="{ $name }" size="{ $size }"/>
)
) else (
(: other inputs :)
<input name="{ $key }" value="{ $value }"/>
)
};
</pre>
declare %rest:path("/host-name") function page:host() {
'Remote host name: ' || request:remote-hostname()
};
</pre>
=File Uploads=
{{Mark|Introduced with Version 7.7:}}
By using the content type {{Code|multipart/form-data}}, uploaded files can be handled server-side using RestXQ. A standard file upload input element can be used to upload files. Suppose you have a simple form (the HTML5 {{Code|multiple}} attribute enables the upload of multiple files):
<pre class="brush:xml">
<form name="myForm" action="/upload" method="POST" enctype="multipart/form-data">
Your Name: <input type="text" name="user-name"/><br/>
<input type="file" name="files[]" multiple="multiple" />
</form>
</pre>
In XQuery, the names and values of the {{Code|input}} elements will be represented as a [[Map module|map]]. Uploaded files (i. e., inputs of type {{Code|file}}) result in a filename and the file content, which is why the value of file inputs will thus be represented in an additional map. The following example stores all uploaded files in a temporary directory and returns XML elements for all inputs:
<pre class="brush:xquery">
declare
%rest:POST("{$data}")
%rest:path("/upload")
function local:upload($data)
{
for $key in map:keys($data)
let $value := $data($key)
return if($val instance of map(*)) then (
(: handles uploaded files :)
for $name in map:keys($value)
let $content := $files($name)
let $size := count(convert:binary-to-bytes($content))
return (
file:write-binary(file:temp-dir() || $name, $content),
<file name="{ $name }" size="{ $size }"/>
)
) else (
(: other inputs :)
<input name="{ $key }" value="{ $value }"/>
)
};
</pre>