File is downloaded instead of being displayed in the browser

你。 提交于 2019-12-20 05:19:16

问题


I have created a brand new app from the luminus app template using lein new luminus my-app +postgres +auth +cljs +swagger. In the generated file src/clj/my_app/routes/home.clj the following compojure route is created:

(GET "/docs" [] (response/ok (-> "docs/docs.md" io/resource slurp)))

When I try to access localhost:3000/docs the file is simply downloaded instead of displayed in the browser. It happens both with Chrome and Safari.

It seems related to ring.util.http-response/ok since I can reproduce the behavior using this route too:

(GET "/hi" [] (response/ok "hi")).

A file "hi" is then downloaded with file content "hi".

Any ideas on what's causing this?


回答1:


Your response handler doesn't set Content-Type for your response body.

You can do it using ring.util.http-response/content-type:

(GET "/hi" [] (-> "hi"
                  (response/ok)
                  (response/content-type "text/plain")))

You can also wrap your handler in ring.middleware.content-type/wrap-content-type so the headers are "guessed" based on the file extension from the URI.



来源:https://stackoverflow.com/questions/35548372/file-is-downloaded-instead-of-being-displayed-in-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!