rails + swfupload 文件上传 session 问题

Posted by wxianfeng Wed, 10 Nov 2010 13:43:00 GMT

环境:ruby 1.8.7 + rails 2.3.5 + swfupload

swfupload 上传文件很方便,支持多文件上传,但是在往往需要做知道这张图片是谁传的,这样就需要取当前登录的session,这样就会出现问题,

因为图片是通过flash传上来的,swfupload里的session和rails里的session不是同一个,理论上属于跨域问题了,所以rails取不到session值,所以必须解决session问题,网络上大家一般都是通过修改session来解决的,即吧session的参数从swfupload里给从传到后台,方可验证通过,eg: http://huacnlee.com/blog/rails-multi-files-upload-with-swfupload

我是通过关闭session,通过参数来验证的,例如传login名字,得到当前是哪个用户:

controller:

class CkeditorController < ApplicationController
skip_before_filter :verify_authenticity_token, :only => [:create]
session :off , :only => [:create]

def create
   @record.user ||= User.find_by_login(params[:login])
end

end

view 页面上:注意中文注释部分

<script>
window.onload = function() {
                upload1 = new SWFUpload({
                        // Backend Settings
                        upload_url: "/ckeditor/create",
                        post_params: {"login" : <%= current_user.login %>,"dir":dir}, // 这里很重要

                        // File Upload Settings
                        file_size_limit : "102400",        // 100MB
                        file_types : "*.*",
                        file_types_description : "All Files",
                        file_upload_limit : 100,
                        file_queue_limit : 0,

                        // Event Handler Settings (all my handlers are in the Handler.js file)
                        swfupload_preload_handler : preLoad,
                        swfupload_load_failed_handler : loadFailed,
                        file_dialog_start_handler : fileDialogStart,
                        file_queued_handler : fileQueued,
                        file_queue_error_handler : fileQueueError,
                        file_dialog_complete_handler : fileDialogComplete,
                        upload_start_handler : uploadStart,
                        upload_progress_handler : uploadProgress,
                        upload_error_handler : uploadError,
                        upload_success_handler : uploadSuccess,
                        upload_complete_handler : uploadComplete,

                        // Button Settings
                        button_image_url : "/images/XPButtonUploadText_61x22.png",
                        button_placeholder_id : "spanButtonPlaceholder1",
                        button_width: 61,
                        button_height: 22,

                        // Flash Settings
                        flash_url : "/javascripts/swfupload/swfupload.swf",
                        flash9_url : "/javascripts/swfupload/swfupload_fp9.swf",


                        custom_settings : {
                                progressTarget : "fsUploadProgress1",
                                cancelButtonId : "btnCancel1"
                        },

                        // Debug Settings
                        debug: false
                });

        }
</script>