0
我使用phoenix框架创建一个网页并创建了一个上传表单,以使用户可以上传一个profil图片。phoenix elixir二进制数据图像
def update(conn, %{"id" => id, "user" => %{"photo" => file}}) do
if(File.exists?(file.path)) do
case File.read(file.path) do
{:ok, body} -> data = IO.iodata_to_binary(body)
changeset = Whiteboard.File.changeset(%Whiteboard.File{}, %{user_id: currentuser.id, name: file.filename , data: data})
这样工作和二进制数据在数据库中是bytea/binary。
现在我的问题:我如何渲染phoenix html.eex文件中的二进制数据以再次显示图像?
编辑:找到一个解决方案
def render("image.html", %{:height => height, :width => width, :data => data, :datatype => datatype}) do
pic = Base.encode64(data)
Phoenix.HTML.raw("<img src=\"data:image/"<>datatype<>";base64,"<>pic<>"\" height=\""<>height<>"\" width=\""<>width<>"\">")
end
所以优点是在性能 - 缓存?你能举个例子吗,怎么这样做? (我只用phoenix/elixir工作2周) – murphy1312
我只是谈论普通的HTTP缓存。如果您使用其他信息来描述文件上次更改的时间或使用etag,则客户端可以缓存该映像,并且可以减少带宽使用量。起初我不担心。 – asonge