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
File upload 使用Elixir中的Acr将文件上载到本地_File Upload_Elixir_Plug - Fatal编程技术网

File upload 使用Elixir中的Acr将文件上载到本地

File upload 使用Elixir中的Acr将文件上载到本地,file-upload,elixir,plug,File Upload,Elixir,Plug,我正在使用Arc.Definition()将图像上载到本地存储 我的文件_service.ex如下: defmodule MyApp.FileService do use Arc.Definition use Arc.Ecto.Definition @image_types ~w(.jpg .jpeg .png .gif) @versions [:original] @default_filename "image.png" @heights %{ mediu

我正在使用Arc.Definition()将图像上载到本地存储

我的文件_service.ex如下:

defmodule MyApp.FileService do
  use Arc.Definition
  use Arc.Ecto.Definition

  @image_types ~w(.jpg .jpeg .png .gif)
  @versions [:original]
  @default_filename "image.png"

  @heights %{
    medium: 400
  }

  @widths %{
    medium: 400
  }

  def __storage, do: Arc.Storage.Local

  def upload_image(%Plug.Upload{} = image, resource_type, resource_id) do
    store({%Plug.Upload{path: image.path, filename: @default_filename},
      %{resource_type: resource_type, resource_id: resource_id}})
  end

  def upload_base64_image(base64_image, resource_type, resource_id) do
    store({%{filename: @default_filename, binary: base64_image_to_binary(base64_image)}})
  end 

  def delete_file(image_url, resource) do
    delete({image_url, resource})
  end

  defp base64_image_to_binary("data:image/" <> rest) do
    rest
    |> String.replace("\n", "")
    |> String.split(",")
    |> Enum.at(1)
    |> Base.decode64!
  end
  defp base64_image_to_binary(base64_image) do
    base64_image
    |> String.replace("\n", "")
    |> Base.decode64!
  end
end
defmodule MyApp.FileService do
使用弧定义
使用Arc.exto.Definition
@图像类型~w(.jpg.jpeg.png.gif)
@版本[:原件]
@默认文件名“image.png”
@高度%{
中等:400
}
@宽度%{
中等:400
}
定义存储,do:Arc.storage.Local
def upload_image(%Plug.upload{}=image,资源类型,资源id)do
存储({%Plug.Upload{path:image.path,filename:@default_filename},
%{resource\u type:resource\u type,resource\u id:resource\u id}})
结束
def upload_base64_映像(base64_映像、资源类型、资源id)do
存储({%{filename:@default_filename,binary:base64_image_to_binary(base64_image)})
结束
def delete_文件(图像url、资源)do
删除({image\u url,resource})
结束
将base64_image_定义为二进制(“data:image/”rest)do
休息
|>字符串。替换(“\n”,”)
|>String.split(“,”)
|>枚举(1)
|>Base.64!
结束
将base64_映像定义为二进制(base64_映像)do
base64_图像
|>字符串。替换(“\n”,”)
|>Base.64!
结束
结束
但是,我得到一个错误,说“Arc.Actions.Store.Store中没有匹配的函数子句”

堆栈跟踪如下所示:

defmodule MyApp.FileService do
  use Arc.Definition
  use Arc.Ecto.Definition

  @image_types ~w(.jpg .jpeg .png .gif)
  @versions [:original]
  @default_filename "image.png"

  @heights %{
    medium: 400
  }

  @widths %{
    medium: 400
  }

  def __storage, do: Arc.Storage.Local

  def upload_image(%Plug.Upload{} = image, resource_type, resource_id) do
    store({%Plug.Upload{path: image.path, filename: @default_filename},
      %{resource_type: resource_type, resource_id: resource_id}})
  end

  def upload_base64_image(base64_image, resource_type, resource_id) do
    store({%{filename: @default_filename, binary: base64_image_to_binary(base64_image)}})
  end 

  def delete_file(image_url, resource) do
    delete({image_url, resource})
  end

  defp base64_image_to_binary("data:image/" <> rest) do
    rest
    |> String.replace("\n", "")
    |> String.split(",")
    |> Enum.at(1)
    |> Base.decode64!
  end
  defp base64_image_to_binary(base64_image) do
    base64_image
    |> String.replace("\n", "")
    |> Base.decode64!
  end
end
**(FunctionClauseError)Arc.Actions.Store.Store/2中没有匹配的函数子句 (arc)lib/arc/actions/store.ex:8:arc.actions.store.store(MyApp.FileService,{%{binary:,文件名:“image.png”})

任何人,请帮助?

您的代码

 def upload_base64_image(base64_image, resource_type, resource_id) do
    store({%{filename: @default_filename, binary: base64_image_to_binary(base64_image)}})
  end 
存储
使用错误

它只接受
元组(文件、范围)
文件路径(映射)

所以它应该是:
store(%%{filename:@default\u filename,binary:base64\u image\u to\u binary(base64\u image)})

请参见github的示例:

# Store a file from a connection body
{:ok, data, _conn} = Plug.Conn.read_body(conn)
Avatar.store(%{filename: "file.png", binary: data})

我通过阅读traceback和arc的store Implementation找到了答案:

  def store(definition, {file, scope}) when is_binary(file) or is_map(file) do
    put(definition, {Arc.File.new(file), scope})
  end

  def store(definition, filepath) when is_binary(filepath) or is_map(filepath) do
    store(definition, {filepath, nil})
  end

你能发布整个stacktrace吗?但是,当我使用元组(文件、范围)或文件路径(映射)时,我得到了另一个错误。(BadMapError)需要一张地图,得到:“image.png”(elixir)lib/map.ex:423:map.get(“image.png”,id,nil)(Absince)lib/Absince/middleware/map_get.ex:9:@Tesythomas这是另一个关于
Absince
而不是
Acr
的问题。这意味着你关于这篇文章的问题已经解决了。