【问题标题】:Setting HTTP response headers with Kemal使用 Kemal 设置 HTTP 响应标头
【发布时间】:2017-05-17 01:33:36
【问题描述】:

我想在凯末尔实现this answer

我当前的设置在app/public/map.pdf 中有一个pdf 文件,在我的主水晶文件中有以下代码:

require "kemal"

#...

get "/map.pdf" do |env|
    env.response.headers["Content-Type"] = "application/pdf"
    env.response.headers["Content-Disposition"] = %(inline;filename="myfile.pdf")
end

Kemal.run

当我通过在浏览器 (firefox) 中打开 localhost:3000/map.pdf 来测试我的代码时,它会提示下载文件(而我希望它尝试显示它)。 curl -I 的结果如下:

HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Kemal
Content-Type: application/octet-stream
ETag: W/"1494983019"
Content-Length: 1170498

我希望看到Content-Type: application/pdf

【问题讨论】:

  • 嗨!首先,我认为stackoverflow.com/questions/20508788/… 可能会有所帮助。其次,您的实际问题是什么?
  • 我刚试过你的代码,它工作。您确定要停止服务器并再次编译应用程序吗?在不重新编译的情况下进行更改将无效。

标签: http crystal-lang kemal


【解决方案1】:

这里是凯末尔作者,

如果标题没问题,您最好选择send_file。但是请确保路由名称和文件不相同。在这种情况下,路由是/pdf

get "/pdf" do |env|
  env.response.headers["Content-Type"] = "application/pdf"
  env.response.headers["Content-Disposition"] = %(inline;filename="map.pdf")
  send_file env, "./public/map.pdf"
end

【讨论】:

  • 做了这些改动后,还是提示下载,而不是content-type是Content-Type: text/html
【解决方案2】:

我希望在哪里看到 Content-Type:application/pdf。

关键是Content-Disposition 不作为标题存在。 也许您出于某种原因根本不发送任何标头。

例如在 PHP 中,如果您的脚本在发送标头之前输出一个字节,则不会发送标头;只有第一个输出之前的标题。 如果您在发送一些数据后设置了 http 标头,则标头将被忽略。

【讨论】:

  • 我不确定你在第二段中想说什么。
  • 您能否详细说明它是如何回答这个问题的?因为我很抱歉,但我认为根本没有。
猜你喜欢
  • 2017-08-28
  • 1970-01-01
  • 2013-08-02
  • 2016-06-04
  • 2012-06-19
  • 1970-01-01
  • 2015-09-29
  • 2012-06-12
  • 2017-08-13
相关资源
最近更新 更多