title: How to make export csv controller in elixir phoenix framework?
body: <p>I have a function that make "contacts.csv" like this.</p>
  
  <pre><code>def write(people) do
      [["name", "phone_number"]]
      |&gt; Stream.concat(people |&gt; Stream.map(&amp;[&amp;1.name, Formatter.remove_international_code(&amp;1.phone_number)]))
      |&gt; CSV.encode()
      |&gt; Enum.into(File.stream!("contacts.csv"))
  end
  </code></pre>
  
  <p>and I want my customer clicks button to download that file. So I did it like this..</p>
  
  <p>In router.ex</p>
  
  <pre><code>get "/csv/:id", CsvExportController, :export
  </code></pre>
  
  <p>In controller file</p>
  
  <pre><code>def export(conn, %{"id" =&gt; phonebook_id}) do
      conn
      |&gt; put_resp_content_type("text/csv")
      |&gt; put_resp_header("content-disposition", "attachment; filename=\"contacts.csv\"")
      |&gt; send_resp(200, export_contacts_from_phonebook(phonebook_id))
    end
  
    def export_contacts_from_phonebook(phonebook_id) do
      Contact.get_people(phonebook_id)
      |&gt; CsvFormatter.write()
    end
  </code></pre>
  
  <p>in CsvFormatter.ex</p>
  
  <pre><code>def write(people) do
      [["name", "phone_number"]]
      |&gt; Stream.concat(people |&gt; Stream.map(&amp;[&amp;1.name, Formatter.remove_international_code(&amp;1.phone_number)]))
      |&gt; CSV.encode()
      |&gt; Enum.sort()
      |&gt; Enum.into(File.stream!("contacts.csv"))
  end
  </code></pre>
  
  <p>and tried but got an error</p>
  
  <pre><code>no function clause matching in Plug.Conn.resp/3
  </code></pre>
  
  <p>how can I do this in controller file?</p>