下载文件,文件名 [英] Download file, filename

查看:108
本文介绍了下载文件,文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为浏览器提供文件下载功能,我可以使用XQuery设置文件名.在此示例中,我将文件名设置为" example.txt ".

I would like to provide a browser with a file download where I set the filename using XQuery. In this example I set the filename to 'example.txt'.

输出文件名是:" docmaker ",这不是我想要的.

The output fiename is: 'docmaker' and this is not what I want it to be.

完成此操作的正确标头配置是什么?

What is the correct header configuration to accomplish this?

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标头或任何其他自定义标头:

Looking at the documentation, the Content-Disposition header, nor any other custom header is supported from within REST extensions:

http://docs.marklogic.com/guide/rest-dev/扩展程序#id_84661

我也看不到其他解决方法.内置文档端点不提供其自身的下载功能,并且转换都通过相同的框架进行,因此也不起作用.

I don't see other ways around this either. The built-in documents endpoint doesn't provide a download feature of its own, and transforms go through the same framework, so that won't work either.

我建议您提交RFE.这可能对您个人没有帮助(您必须等待下一个版本),但将来可能对其他人有用.

I'd recommend filing an RFE. That will probably not be helpful for you personally (you'll have to wait for one of the next releases), but may be useful for others in the future..

**更新**

@mblakele的建议在一个有效的示例下面起作用.我不愿意推荐它. add-response-header有效,但是set-response-code不起作用. REST-API将覆盖它.将来,使用add-response-header调用可能会发生同样的情况.

@mblakele's suggestion works, below a working example. I'm reluctant recommending it though. The add-response-header works, but the set-response-code doesn't. The REST-API will override it. The same could happen in future with the add-response-header call..

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!

这篇关于下载文件,文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆