How to Upload Files Using Post Controller: Difference between revisions
From ElixirBlocks
(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 |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
''' | |||
Router''' | |||
<source> | |||
live "/sandbox", SandboxLive, :home | |||
live "/sim-app", SimappLive, :home | |||
</source> | |||
Create a LiveView and render a form with the multipart attribute. | Create a LiveView and render a form with the multipart attribute. | ||
Line 14: | Line 19: | ||
/> <input type="submit" /> | /> <input type="submit" /> | ||
</.form> | |||
</source> | </source> | ||
Latest revision as of 17:11, 13 June 2024
Router
live "/sandbox", SandboxLive, :home live "/sim-app", SimappLive, :home
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