2011-10-10 66 views
1

我只是想用uploadify和回形針上傳一個文件。uploadify和回形針3.1.01

這是索引視圖:

<%- session_key = Rails.application.config.session_options[:key] -%> 

$('#upload').uploadify({ 
    'uploader' : 'uploadify.swf', 
    'script' : '/videos', 
    'cancelImg' : 'images/cancel.png', 
    'buttonText' : 'Add video!', 
    'folder' : '/public', 
    'auto'  : true, 
    'scriptData' : {"<%= key = Rails.application.config.session_options[:key] %>" : "<%= cookies[key] %>", 
     "<%= request_forgery_protection_token %>" : "<%= form_authenticity_token %>", 
    }, 
    onError : function (event, id, fileObj, errorObj) { 
     alert("error: " + errorObj.info); 
    } 
}); 

這是我的模型:

class Video < ActiveRecord::Base 

    has_attached_file :source 

    validates_attachment_presence :source 

end 

...這是我的移民文件:

class CreateVideos < ActiveRecord::Migration 
def self.up 
    create_table :videos do |t| 
    t.string :source_content_type 
    t.string :source_file_name 
    t.integer :source_file_size 
    t.timestamps 
end 
end 

def self.down 
    drop_table :videos 
end 
end 

現在,當我上載一個文件,沒有引發錯誤,無論是客戶端還是服務器端,但它看起來像回形針沒有得到由uploadify發送給他的參數。事實上,如果我從模型中刪除驗證,則會創建不帶參數的資源。

在控制器I添加:

def create 
logger.info(params.inspect) (1) 
@video = Video.new(params[:video]) 
@video.save 
logger.info @video.inspect (2) 
end 

在development.log(1)給我

{"Filename"=>"prova.mov", "authenticity_token"=>"9q5w5LipGoBj Iac055OPbDofFU5FMbpEX675 RqOqs=", "folder"=>"/public", "_StreamVideo_Api_session"=>... 

(2)未經驗證

#<Video id: 1, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: "2011-10-10 12:12:38", updated_at: "2011-10-10 12:12:38"> 

(2)與驗證

#<Video id: nil, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: nil, updated_at: nil> 

問題出在哪裏? 預先感謝您的答案!

在我看來,我把:

<h1>New video</h1> 

<% form_for :video, :html => { :multipart => true } do |f| %> 
    <%= f.file_field :source%> 
<% end %> 

http://localhost:3000/videos/new顯示我只有標題!

回答

0

該代碼看起來不錯,你應該只添加encodeURI函數。在Uploadify 2.1 它應該工作有:

'scriptData' : { 
    $('meta[name=csrf-param]').attr('content') : encodeURI(encodeURIComponent($('meta[name=csrf-token]').attr('content'))) 
    } 

隨着Uploadify 3.1:

'formData': { 
     $('meta[name=csrf-param]').attr('content') : encodeURI($('meta[name=csrf-token]').attr('content')) 
    }