我想提供一个带有文件下载功能的浏览器,我可以使用XQuery
设置文件名。在此示例中,我将文件名设置为“example.txt”。
输出文件名是:“docmaker”,这不是我想要的。
完成此操作的正确 header 配置是什么?
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 header ,也不支持任何其他自定义 header :
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 }
}
}
};
呵呵!
关于marklogic - 下载文件,文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24606676/