2011-07-03 34 views
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. 

回答

0

上傳文件非常棘手。我建議使用Uploadify。有一個如何在這裏執行的例子:https://github.com/rdetert/FormFly

另外要注意的是Rails使用的protect_from_forgery。有一個修復不是很久以前,你需要包括這application.js中獲得AJAX請求在正常工作:

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

這是不可能上傳通過AJAX文件,但也有變通辦法和插件來幫助。

查看這裏的一些答案:jQuery Ajax File Upload