Changes

Jump to navigation Jump to search
1,512 bytes added ,  09:33, 28 July 2013
no edit summary
};
</pre>
 
=File Upload=
 
{{Mark|Introduced with Version 7.7:}}
 
By using the content type {{Code|multipart/form-data}} uploaded files can be handled at server-side using RestXQ. A user is therefore able to upload files using a standard file upload input element. Suppose you have a simple form:
 
<pre>
<form name="myForm" action="/upload" method="POST" enctype="multipart/form-data">
<input type="hidden" name="example1" value="test" />
<input type="file" name="files[]" multiple="multiple" />
</form>
</pre>
 
All input elements will be put into a [[#Map module|map]] by using the element name as key. File elements will be treated differently as multiple files can be uploaded at once using the {{Code|multiple}} attribute. All files will be put into a map using the file name as key and this map is accessible using {{Code|files}} as key of the original map. See the following example to store all uploaded files into a specific directory:
 
<pre class="brush:xquery">
declare
%restxq:POST("{$data}")
%restxq:path("/upload")
function page:upload($data)
{
let $example1 := $data('example1')
let $files := $data('files')
for $name in map:keys($files)
let $content := $files($name)
let $size := count(convert:binary-to-bytes($content))
return file:write-binary("files/", $content)
};
</pre>
 
The value of the hidden form element {{Code|example1}} is stored into {{Code|$example1}}. Additionally, you can calculate the size of the uploaded file using {{Code|count(convert:binary-to-bytes($content))}}.
=References=
administrator, editor
28

edits

Navigation menu