MarkLogic XQuery MultiUpload
Some jQuery and XQuery code to load content into a MarkLogic database:
'form#uploadForm': function(idx, elm) { var form = $(elm) form.submit(function(evt) { evt.preventDefault(); rn_util.log('SUBMITTED: ', form); $.ajax({ url: 'spike/uploadController.xqy', type: 'POST', data: new FormData(elm), cache: false, contentType: false, processData: false, beforeSend: function(jqXHR, settings) { rn_util.log('beforeSend', jqXHR.upload); }, success: function(data, textStatus, jqXHR) { rn_util.log('POST COMPLETED: ', textStatus, '; ', jqXHR); } }); }); }
xquery version "1.0-ml"; declare namespace zip = "xdmp:zip"; declare namespace aib = "co.wlv.xml.aib"; xdmp:log("GREETINGS:UPLOAD"), for $headerName as xs:string in xdmp:get-request-header-names() return xdmp:log(concat("HEADERNAME: ", $headerName, " = ", xdmp:get-request-header($headerName))), for $fieldName as xs:string in xdmp:get-request-field-names() return xdmp:log(concat("FIELDNAME: ", $fieldName)), for $inputFile as xs:string in xdmp:get-request-field-filename("inputFiles") return xdmp:log(concat("FILENAME: ", $inputFile)), let $fileNames as xs:string* := xdmp:get-request-field-filename("inputFiles"), $inventory as element(aib:inventory) := doc("/packages/inventory.xml")/aib:inventory for $inputFile as item() at $i in xdmp:get-request-field("inputFiles") let $fileName as xs:string := $fileNames[$i], $packageUri as xs:anyURI := xs:anyURI(concat("/packages/package-", xdmp:random())), $zipManifest as node() := xdmp:zip-manifest($inputFile) return ( xdmp:log(concat("FILE: ", $fileName, "; ", xdmp:quote($zipManifest), " COUNT: ", count($zipManifest/zip:part))), xdmp:node-insert-child($inventory, element {QName("co.wlv.xml.aib", "package")} { attribute uri {$packageUri}, attribute name {$fileName}, attribute added {current-dateTime()}, for $part as element(zip:part) in $zipManifest/zip:part let $file as node()+ := xdmp:zip-get($inputFile, string($part)), $XXX := xdmp:log(xs:anyURI(concat($packageUri, "/", replace(string($part), "\\", "/")))), $fileUri as xs:anyURI := xs:anyURI(concat($packageUri, "/", replace(string($part), "\\", "/"))) return ( xdmp:log(concat("PART: ", string($part), "; COUNT: ", count($file))), xdmp:document-insert($fileUri, $file[1]), element {QName("co.wlv.xml.aib", "file")} { attribute uri {$fileUri}, attribute size {$part/@uncompressed-size}, $part/@last-modified } ) }) )