文件已下载,而不是显示在浏览器中 [英] File is downloaded instead of being displayed in the browser

查看:137
本文介绍了文件已下载,而不是显示在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 lein new luminus my-app + postgres + auth + cljs + swagger 从luminus应用程式范本建立了全新的应用程式。在生成的文件 src / clj / my_app / routes / home.clj 中创建以下compojure路由:



(GET/ docs[](response / ok( - >docs / docs.mdio / resource slurp)))



当我尝试访问 localhost:3000 / docs 时,文件只是下载而不是显示在浏览器中。



它似乎与 ring.util.http-response / ok 有关我可以使用这个路由再现行为:



(GET/ hi[](response / okhi)) code>。



然后下载文件hi,文件内容为hi。



解决方案

您的回应处理常式并未设定 Content-Type



你可以使用 ring.util.http-response / content-type

 (GET/ hi[]( - >hi
response / ok)
(response / content-typetext / plain)))

您还可以在 ring.middleware.content-type / wrap-content-type ,因此根据来自URI的文件扩展名,猜测 >

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?

解决方案

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.

这篇关于文件已下载,而不是显示在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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