Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 凤凰仙丹二值数据图像_Image_Elixir_Phoenix Framework_Bytea - Fatal编程技术网

Image 凤凰仙丹二值数据图像

Image 凤凰仙丹二值数据图像,image,elixir,phoenix-framework,bytea,Image,Elixir,Phoenix Framework,Bytea,我使用phoenix框架创建了一个网页,并创建了一个上传表单,让用户能够上传一张profil图片 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_bin

我使用phoenix框架创建了一个网页,并创建了一个上传表单,让用户能够上传一张profil图片

    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
def render(“image.html”,%{:height=>height,:width=>width,:data=>data,:datatype=>datatype})执行
pic=Base.encode64(数据)
raw(“数据类型”base64,“pic“\”高度=\”高度“\”宽度=\”宽度“\”>”)
结束

虽然浏览器支持添加base64图像URL,但我建议您采用缓存更容易使用的传统方法,并创建一个新的Phoenix路由来提供图像

为此,只需发出正确的mime类型头,而不是呈现模板,只需将列的内容直接放入连接中即可


您可能应该检查前几个字节,看看它是否匹配已知格式的白名单。您可以在这里搜索这些字节:

那么性能缓存的优势是什么?你能给我举个例子,怎么做?(sry我只在phoenix/elixir上工作了2周)我只是在谈论正常的HTTP缓存。如果您使用其他信息来描述上次更改文件的时间或使用etag,则客户端可以缓存该图像,并且您可以减少带宽使用。一开始我不会担心的。