0
So I need to upload files via AJAX...

I have both parts working seperately....

Upload View:

<%= form_tag('upload_zip', :multipart => true) do %>
  <p>
    File:<br />
    <%= file_field_tag 'zip_file' %><br />
  </p>
  <p>
    <%= submit_tag "Upload ZIP/TAR.GZ" %>
  </p>
<% end %>

And else where I have are Remote form_tag working...

<%= form_tag url_for(:action => :add_tracks), :remote => true do %>
.....
<%= submit_tag "Submit", :disable_with => "Processing..." %>


Both of these work fine but when I try to add :remote => true I can't seem to get it to work.  Most of the syntaxes seem to not get the file name and try to upload nil.
Braden
  • 1
  • 1

2 Answers2

0

File uploads are tricky. I'd recommend using Uploadify. There is an example of how to do it here: https://github.com/rdetert/FormFly

The other thing to watch out for is the protect_from_forgery that Rails uses. There was a fix not too long ago where you needed to include this in application.js to get AJAX requests to work properly in:

$(document).ajaxSend(function(e, xhr, options) {
  var token = $("meta[name='csrf-token']").attr("content");
  xhr.setRequestHeader("X-CSRF-Token", token);
});
Dex
  • 12,527
  • 15
  • 69
  • 90
0

It's not possible to upload a file via AJAX, but there are work-arounds and plugins to help.

See some of the answeres here: jQuery Ajax File Upload

Community
  • 1
  • 1
Wizard of Ogz
  • 12,543
  • 2
  • 41
  • 43