1

I want implement this answer in Kemal.

My current setup has a pdf file in app/public/map.pdf, and the following code in my main crystal file:

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

When I test my code by opening localhost:3000/map.pdf in a browser (firefox), it prompts to download the file (while I want it to attempt to display it). And curl -I results in the following:

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

Where I would hope to see Content-Type: application/pdf.

Community
  • 1
  • 1
Jones
  • 1,154
  • 1
  • 10
  • 35
  • Hi! Firstly, I think http://stackoverflow.com/questions/20508788/do-i-need-content-type-application-octet-stream-for-file-download could be helpful. Secondly, what is your actual question? – Eel Lee May 18 '17 at 13:56
  • I just tried your code and it worked. Are you sure you are stopping the server and compiling again the application? Making changes without recompiling will have no effect. – asterite May 22 '17 at 13:08

2 Answers2

3

Kemal author here,

If the headers are ok you should be good to go with send_file. However be sure that the route name and file are not the same. In this case the route is /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
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
  • With those changes, it still prompts to download, and not content-type is `Content-Type: text/html`. – Jones May 17 '17 at 23:27
-2

Where I would hope to see Content-Type: application/pdf.

The point is that Content-Disposition doesn't exist as a header. Maybe you don't send any headers at all for some reason.

For example in PHP, if your script outputs a single byte before it sends the headers then no headers will be sent; only the headers before the first output. If you set a http header after some data is sent then the header will be ignored.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
kapad
  • 52
  • 7