1. Uploading Files in HTML Forms

You can upload files from Forms using the content type of multipart/form-data. The HTML5 attribute multiple allows you upload multiple files in one go. Consider the following HTML Form

<form action="/upload" method="POST" enctype="multipart/form-data">
  <input type="file" name="files" multiple="multiple"/>
  <input type="submit"/>

Files uploaded via a Form Parameter like this need to be routed through to a MarkLogic map:map object. See the following example which could be use in conjunction with the above HTML Form

  %rest:form-param("files", "{$files}")
function upload($files as map:map) {

2. The map:map structure

  • The map's keys represent the filenames of each file uploaded
  • The map's values are themselves maps too, let's call this an "entry map"
  • The entry map contains two keys, a content-type and a body
  • The value for content-type is the Content Type of this particular file
  • The value for body is the Content Body of this particular file as a binary() item

The following example iterates through uploaded files, getting each map:map entry and then the Content Type as well as the Body of that file.

  %rest:form-param("files", "{$files}")
function example-upload($files as map:map) {
  for $filename in map:keys($files) (: iterate through files :)
  let $entry-map as map:map := map:get($files, $filename) (: a map per file :)
    let $content-type as xs:string? := map:get($entry-map, "content-type")
    let $body as binary() := map:get($entry-map, "body")
  return ()