How to import users from csv file with elixir/phoenix?

送分小仙女□ 提交于 2020-01-06 17:58:52

问题


I have a csv file with a list of users and the following import method inside UserController and I would like to import these users by submitting the csv file with a form. And It looks like I'm doing it wrong.

users_controller

  def import(conn, %{"user" => user_params}) do
    user_params["file"]
    |> File.stream!()
    |> CSV.decode
    |> Enum.each(fn(user) -> User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert end)
    conn
    |> put_flash(:info, "Imported")
    |> redirect(to: user_path(conn, :index))
  end

routes

post "/import", UsersController, :import, as: :import_csv

form

<%= render "import_form.html", changeset: @changeset,
                        action: import_csv_path(@conn, :import) %>

-

<%= form_for @changeset, @action, [multipart: true], fn f -> %>
  <div class="form-group">
    <%= label f, :file, class: "control-label" %>
    <%= file_input f, :file %>
  </div>

  <div class="form-group">
    <%= submit "Submit", class: "btn btn-primary" %>
  </div>
<% end %>

model

  schema "users" do
    field :name, :string
    field :email, :string
    field :file, :any, virtual: true
    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:name, :email, :file])
    |> validate_required([:name, :email])
    |> unique_constraint(:email)
  end

The following code works from iex

  def import(file) do
    file
    |> File.stream!()
    |> CSV.decode
    |> Enum.each(fn(user) -> User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert end)
  end

回答1:


You don't get a file back in your params. Here are the params I get back for a multipart file

[warn] module=UcxChat.AttachmentController line=10 function=create/2  attachment params: %{"channel_id" => "300233dd-782f-4718-9eed-00b8cc412a79", "description" => "", "file" => %Plug.Upload{content_type: "text/plain", filename: "test.txt", path: "/var/folders/wt/3q11kty15rqfb5v9rpqg0ssm0000gn/T//plug-1494/multipart-421483-239132-3"}, "file_name" => "test.txt", "type" => "text/plain", "user_id" => "427452eb-c9cf-457b-9c55-0904c9d24385"}

You can see that the params["file"] is a Plug.upload struct.

To get the actual file, you should get the :path field like:

  def import(conn, %{"user" => user_params}) do
    user_params["file"].path
    |> File.stream!()
    |> CSV.decode
    |> Enum.each(fn(user) -> User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert end)
    conn
    |> put_flash(:info, "Imported")
    |> redirect(to: user_path(conn, :index))
  end

You probably also want to do some error handling. Something like:

def import(conn, %{"user" => user_params}) do
  user_params["file"].path
  |> File.stream!()
  |> CSV.decode
  |> Enum.map(fn(user) -> 
    User.changeset(%User{}, %{name: Enum.at(user, 0), email: Enum.at(user, 1)}) |> Repo.insert 
  end)
  |> Enum.filter(fn 
    {:error, cs} -> true
    _ -> false
  end)
  |> case do
    [] -> 
      conn
      |> put_flash(:info, "Imported")
      |> redirect(to: user_path(conn, :index))
    errors -> 
      errors = parse_errors(errors)  # create this fun 
      conn
      |> put_flash(:erorr, errors)
      |> render("import.html")
  end
end


来源:https://stackoverflow.com/questions/43891367/how-to-import-users-from-csv-file-with-elixir-phoenix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!