Jump to navigation Jump to search
1,512 bytes added ,  10:33, 28 July 2013
no edit summary
=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:
<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" />
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">
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)
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))}}.
administrator, editor


Navigation menu