.ajax() interface is used to perform asynchronous requests to the server. In the settings parameter you can specify additional data that needs to be sent to the server with the request. But if you try and send a file, you will notice that it won’t work! Here is a trick to solve that problem.
Let’s say you have an input element (with an id of
image_input) on the page. You can access the chosen file like so:
But as mentioned above, you cannot directly pass it as your ajax data. Instead, you will need to wrap it in a
1 2 3 4 5 6 7 8 9 10 11 12 13
Now, your file is properly embedded in request data. On the (Rails) server side, you can access this data with
params[:file] and do what you want with it.