I have a form in a Phoenix LiveView that contains a file_input
. I want to use it to allow a user to upload an image. I'm having trouble understanding what the form is sending to my backend, and what I can do with it. I expected a %Plug.Upload{} representation of the image file, as described in documentation, but instead I just get "[object File]"
.
Note that I am not backing the form with a changeset, because I am not using Ecto:
<%= f = form_for :post, "#", phx_submit: :create_post, phx_change: :image_attach, multipart: true %>
<%= hidden_input f, :user_id, value: @current_user.account_id %>
<%= textarea f, :message, class: "social-post-box", placeholder: "Something on your mind?" %>
<div class="post-submit-container">
<%= submit "Post", class: "post-submit" %>
<label for="post_image" class="post-submit-image"></label
<%= file_input f, :image %
</div>
</form>
I have a handler in the LiveView module to handle the submitted form, and when I inspect the image upload I see "[object File]"
def handle_event("create_post", %{"post" => post_params}, socket) do
IO.inspect post_params["image"]
{:noreply, socket}
end
I tried prying at this location so that I could run i post_params["image"]
, and it explains that the object is a bitstring, ie just a binary. So it's literally just the text "[object File]", and not even a file at all?
What is it that I am receiving from my form? Why isn't it a %Plug.Upload{}
? How can I achieve my goal of saving this image upload to the local filesystem?