在控制器中渲染静态html页面

时间:2016-06-20 14:01:17

标签: elixir phoenix-framework

有没有办法读取和呈现位于的静态html文件 控制器中服务器上的另一部分?我不打算通过静态页面功能重定向或提供此页面。

2 个答案:

答案 0 :(得分:7)

您应该使用Plug.Conn.send_file/5。此函数将比将整个文件读入内存然后使用Phoenix.Controller.html/2发送文件更有效地发送文件内容:

conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> Plug.Conn.send_file(200, "/path/to/html")

请注意,我必须手动添加content-type标头才能获得与Phoenix.Controller.html/2相同的行为。

答案 1 :(得分:2)

您可以使用Phoenix.Controller.html/2功能发送自定义html内容。使用File.read!/2读取文件并将内容发送给客户端。

def index(conn, _params) do
  html(conn, File.read!("path/to/file.html"))
end

希望这有帮助。

相关问题