Skip to content

Uploading file using rails.js

Pan Gaoyong edited this page Apr 20, 2017 · 5 revisions

By default, old browsers do not allow files to be uploaded via AJAX(latest versions support). As a result, if there are any non-blank file fields in the remote form, this adapter aborts the AJAX submission and allows the form to submit through standard means.

The ajax:aborted:file event allows you to bind your own handler to process the form submission however you wish.

     $('form').bind('ajax:aborted:file', function(event, elements){
      // Implement own remote file-transfer handler here for non-blank file inputs passed in `elements`.
      // Returning false in this handler tells rails.js to disallow standard form submission
       return false;
     });

The ajax:aborted:file event is fired when a file-type input is detected with a non-blank value.

Third-party tools can use this hook to detect when an AJAX file upload is attempted, and then use techniques like the iframe method to upload the file instead. One of those tools is Remotipart.