marklogic - 下载文件,文件名

标签 marklogic

我想提供一个带有文件下载功能的浏览器,我可以使用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/

相关文章:

optimization - MarkLogic XQuery 尾调用优化

gradle - 调度MarkLogic数据中心流/自定义步骤的最佳方法

xquery - 如何在marklogic中列出目录中所有文档的所有URI

nosql - MarkLogic:在单个事务中提交多个语句

logging - MarkLogic - XDMP-NEWSTAMP 异常

javascript - 如何在 MarkLogic JSON 中搜索文件中特定路径处的键值?

xml - 在 marklogic 中的多个集合之间查询时优化记录的检索

marklogic - 如何计算两个 xs :dates? 之间的年份和月份

MarkLogic 重新索引问题

rest - Marklogic REST API - 检索文件内容