Upload file via Ajax

I’m trying to upload a file via ajax and I get the following error

[debug] ** (Phoenix.NotAcceptableError) no supported media type in accept header, expected one of ["html"]
    (phoenix) lib/phoenix/controller.ex:961: Phoenix.Controller.refuse/2
    (phoenix) lib/phoenix/controller.ex:921: Phoenix.Controller.parse_header_accept/4
    (myapp) web/router.ex:14: myapp.Router.protected/2
    (myapp) web/router.ex:1: myapp.Router.match_route/4
    (myapp) web/router.ex:1: myapp.Router.do_call/2
    (myapp) lib/prevensys/endpoint.ex:1: myapp.Endpoint.phoenix_pipeline/1
    (myapp) lib/plug/debugger.ex:123: myapp.Endpoint."call (overridable 3)"/2
    (myapp) lib/myapp/endpoint.ex:1: myapp.Endpoint.call/2
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
    (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

My html file is:

<form name="upload" id="upload"  class="dropzone"
                enctype="multipart/form-data" accept-charset="utf-8"></form>

<script type="text/javascript">

Dropzone.autoDiscover = false;

$(function() {

  $("#upload").dropzone({
        url: '<%= employee_path(@conn, :upload_files)%>'
    });
})
</script>

my action

  def upload_files(conn, %{"user" => user_params}) do
     IO.inspect user_params
  end

what would be the correct way to load the file?

Based on the description it sounds like it is not setting ‘html’ as a supported return type (usually you display an html or so page at the end). If you are expected JSON back then be sure to set that mimetype (it might be what your dropzone call is already doing?) and make sure it is going to an api endpoint (which already accepts JSON) or somewhere in your router that accepts JSON.

My router.ex contains the following

  pipeline :browser do
    plug :accepts, ["html","json"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

already solved it, thanks, I have to add

plug: accepts ["html", "json"],

Thanks!

1 Like