使用Arc.Ecto上传

时间:2016-02-29 13:10:48

标签: elixir phoenix-framework

我目前正在与Phoenix合作创建一个小型API。除文件上传外,一切都很好。

以下是我在日志中的内容:

Parameters: %{"picture_version" => %{"file" => %Plug.Upload{content_type: "image/png", filename: "spectrum.png", path: "/tmp/awea/plug-1456/multipart-745660-733498-2"}, "usage" => "main"}}

然后是Arc库中的错误:

Server: localhost:4000 (http) Request: POST /api/picture_versions ** (exit) an exception was raised: ** (ArgumentError) argument error lib/arc/actions/store.ex:39: anonymous fn/1 in Arc.Actions.Store.handle_responses/2 (elixir) lib/enum.ex:704: anonymous fn/3 in Enum.filter/2 (elixir) lib/enum.ex:1385: Enum."-reduce/3-lists^foldl/2-0-"/3 (elixir) lib/enum.ex:704: Enum.filter/2 lib/arc/actions/store.ex:39: Arc.Actions.Store.handle_responses/2 lib/arc_ecto/type.ex:5: Arc.Ecto.Type.cast/2 (ecto) lib/ecto/type.ex:590: Ecto.Type.cast/2 (ecto) lib/ecto/changeset.ex:486: Ecto.Changeset.cast_field/6 (ecto) lib/ecto/changeset.ex:418: Ecto.Changeset.do_process_param/8 (elixir) lib/enum.ex:1102: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3 (ecto) lib/ecto/changeset.ex:382: Ecto.Changeset.cast/5 (ecto) lib/ecto/changeset.ex:353: Ecto.Changeset.cast/4 (copier_creer_web) web/controllers/api/picture_version_controller.ex:10: CopierCreerWeb.API.PictureVersionController.create/2 (copier_creer_web) web/controllers/api/picture_version_controller.ex:1: CopierCreerWeb.API.PictureVersionController.action/2 (copier_creer_web) web/controllers/api/picture_version_controller.ex:1: CopierCreerWeb.API.PictureVersionController.phoenix_controller_pipeline/2 (copier_creer_web) lib/phoenix/router.ex:261: CopierCreerWeb.Router.dispatch/2 (copier_creer_web) web/router.ex:1: CopierCreerWeb.Router.do_call/2 (copier_creer_web) lib/copier_creer_web/endpoint.ex:1: CopierCreerWeb.Endpoint.phoenix_pipeline/1 (copier_creer_web) lib/plug/debugger.ex:93: CopierCreerWeb.Endpoint."call (overridable 3)"/2 (copier_creer_web) lib/phoenix/endpoint/render_errors.ex:34: CopierCreerWeb.Endpoint.call/2

我已按照arcarc.ecto的自述文件实现了它。我和菲尼克斯很新,所以我不完全知道我在做什么^^

编辑,控制器代码

    defmodule CopierCreerWeb.API.PictureVersionController do
      use CopierCreerWeb.Web, :controller

      alias CopierCreerWeb.PictureVersion

      # plug :scrub_params, "picture_version" when action in [:create]

      def create(conn, %{"picture_version" => picture_version_params}) do
        changeset = PictureVersion.changeset(%PictureVersion{}, picture_version_params)

        if changeset.valid? do
          picture_version = Repo.insert!(changeset)

          conn
          |> put_status(:created)
          |> render("show.json", picture_version: picture_version)
        else
          conn
          |> put_status(:unprocessable_entity)
          |> render(CopierCreerWeb.ChangesetView, "error.json", changeset: changeset)
        end
      end

      def delete(conn, %{"id" => id}) do
        picture_version = Repo.get(PictureVersion, id)

        # Here we use delete! (with a bang) because we expect
        # it to always work (and if it does not, it will raise).
        Repo.delete!(picture_version)

        send_resp(conn, :no_content, "")
      end
    end

编辑,上传者配置

      defmodule CopierCreerWeb.File do
    use Arc.Definition
    use Arc.Ecto.Definition

    @versions [:original]

    def __storage do Arc.Storage.Local end
  end

1 个答案:

答案 0 :(得分:0)

经过多次测试和一个全新的应用程序。似乎问题来自Arc 0.4.0并且它在0.4.1中得到了解决。