我想为浏览器提供文件下载功能,我可以使用来设置文件名XQuery
。在此示例中,我将文件名设置为“ example.txt ”。
输出文件名是:“ docmaker ”,这不是我想要的。
完成此操作的正确标头配置是什么?
declare
%roxy:params("ID=xs:number")
function strlf:get(
$context as map:map,
$params as map:map
) as document-node()*
{
map:put($context, "output-types", "application/csv"),
map:put($context, "Content-Disposition", 'attachment; filename="example.txt"'),
xdmp:set-response-code(200, "OK"),
document {
try {
let $id := map:get($params,"ID")
let $query :=
if (fn:empty($id))
then ()
else cts:element-range-query(xs:QName("jbasic:ID"),"=",(fn:number($id)))
for $doc in cts:search(fn:doc(), cts:and-query((cts:directory-query("/app/docmaker/"),$query)), ('unfiltered'))
return $doc//jbasic:Text/text()
}
catch ($e) {
element error { $e/error:message }
}
}
};
查看文档,REST扩展内支持Content-Disposition标头或任何其他自定义标头:
http://docs.marklogic.com/guide/rest-dev/extensions#id_84661
我也看不到其他解决方法。内置文档端点不提供其自身的下载功能,并且转换都经过相同的框架,因此也不起作用。
我建议您申请RFE。这可能对您个人没有帮助(您必须等待下一个版本),但将来可能对其他人有用。
**更新**
@mblakele的建议在一个有效的示例下面起作用。我不愿意推荐它。add-response-header有效,但是set-response-code不起作用。REST-API将覆盖它。将来,使用add-response-header调用可能会发生同样的情况。
xquery version "1.0-ml";
module namespace ext = "http://marklogic.com/rest-api/resource/download";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
declare namespace roxy = "http://marklogic.com/roxy";
declare namespace xs = "http://www.w3.org/2001/XMLSchema";
declare option xdmp:mapping "false";
declare
%roxy:params("ID=xs:number")
function ext:get(
$context as map:map,
$params as map:map
) as document-node()*
{
map:put($context, "output-types", "application/csv"),
xdmp:add-response-header("Content-Disposition", 'attachment; filename="example.txt"'),
xdmp:set-response-code(300, "OK"),
document {
try {
doc()[1]
} catch ($e) {
element error { $e/error:message }
}
}
};
HTH!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句