How to Upload Files Using Post Controller: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
(Created page with " Create a LiveView and render a form with the multipart attribute. <source> <.form :let={f} action={~p"/transmit/#{switch}"} multipart> <.input field={f[:switch_item]} name="switch" type="file" /> <input type="submit" /> </.form> </source> Create a controller for the route /transmit/#{switch} '''transmit_controller.ex''' <source> defmodule AppWeb.TransmitCont...")
 
No edit summary
Line 14: Line 14:


                 /> <input type="submit" />
                 /> <input type="submit" />
              </.form>
    </.form>
</source>
</source>



Revision as of 17:10, 13 June 2024


Create a LiveView and render a form with the multipart attribute.


   <.form :let={f} action={~p"/transmit/#{switch}"} multipart>


                <.input
                  field={f[:switch_item]}
                  name="switch"
                  type="file"

                /> <input type="submit" />
     </.form>


Create a controller for the route /transmit/#{switch}


transmit_controller.ex

defmodule AppWeb.TransmitController do
  use AppWeb, :controller


  def home(conn, params) do

    IO.inspect params

    content = params["switch"].path

    IO.inspect"______________________________"

    case File.write("priv/my_file.json", content) do
      :ok ->
        IO.puts("File written successfully.")

      {:error, reason} ->
        IO.puts("Failed to write file: #{reason}")
    end

    redirect(conn, to: "/sim-app")
  end
end