1
votes

I want to write a xquery which will download & save the uploaded documents in Marklogic Server. How can I do this ?

2

2 Answers

3
votes

See http://developer.marklogic.com/pubs/5.0/apidocs/AppServerBuiltins.html#xdmp:get-request-field, which has the following example

Consider a form.xqy XQuery module with the following content:

<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
  <form name="test" action="upload.xqy?uid={xdmp:random()}" method="post"
        enctype="multipart/form-data">
  <p><label>File to upload:
  <input type="file" class="name" name="upload" size="50"/></label></p>
  <p><input type="submit" value="Upload and Get Results"/></p>
  </form>
  </body>
</html> 

Then have an upload.xqy XQuery module as follows:

let $filename := xdmp:get-request-field-filename("upload")
let $disposition := fn:concat("attachment; filename=""",$filename,"""")
let $x := xdmp:add-response-header("Content-Disposition", $disposition)
let $x:= xdmp:set-response-content-type(
         xdmp:get-request-field-content-type("upload"))
return
xdmp:get-request-field("upload")

Execute the form.xqy file, select a file, and click the "Upload and Get Results" button. The file you uploaded will open according to the mime type the browser. If you wanted to save it to the database, you could use xdmp:document-insert to do so.

There is additional work to support multipart uploads.

1
votes

Use xdmp:add-response-header.

 let $uri := xdmp:get-request-field('uri', '')
 let $filename := fn:tokenize($uri, "/")[fn:last()]
 return 
 (
     xdmp:add-response-header("Content-Disposition", fn:concat("attachment; filename=", $filename)),
     fn:doc($uri)
 )