2011-07-03 7 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です。

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

それはAJAX経由でファイルをアップロードすることはできませんが、回避策があります:あなたはAJAX要求がで正しく動作させるためにapplication.jsで、これを含めることが必要な場合に修正が長すぎる前にはありませんでしたそして助けになるプラグイン。

こちらの回答はこちらjQuery Ajax File Upload

関連する問題