简体   繁体   English

从Flex上传文件到Rails 2服务器

[英]File Upload from Flex to Rails 2 server

使用默认的Cookie会话存储时,是否有任何方法可以将文件从Flex应用程序上传到Rails 2.2后端?

No. The reason it doesn't work is because Flex does not transmit the cookies when using FileReference#upload. 不能。之所以不起作用,是因为使用FileReference#upload时Flex不会传输cookie。 A workaround (for Rails 2.3) is to insert a custom middleware handler for flash requests that takes arguments in the query string and adds them to the HTTP_COOKIE environment before it reaches Rails. 一种解决方法(对于Rails 2.3)是为Flash请求插入一个自定义的中间件处理程序,该处理程序在查询字符串中获取参数,并在到达Rails之前将其添加到HTTP_COOKIE环境中。

require 'rack/utils'

class FlashSessionCookieMiddleware
  def initialize(app, session_key = '_session_id')
   @app = app
   @session_key = session_key
  end

  def call(env)
    if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
      params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
      env['HTTP_COOKIE'] = [ @session_key, params[@session_key] ].join('=').freeze unless params[@session_key].nil?
    end
    @app.call(env)
  end
end

Make sure the file is in your load path and add it to your session_store.rb: 确保文件在您的加载路径中,并将其添加到session_store.rb:

ActionController::Dispatcher.middleware.use FlashSessionCookieMiddleware, ActionController::Base.session_options[:key]

Then you'll need to output the session key to a view somewhere and load it using ExternalInterface inside of Flex: 然后,您需要将会话密钥输出到某个地方的视图,并使用Flex内部的ExternalInterface加载它:

def upload_path_with_session_information
  session_key = ActionController::Base.session_options[:key]
  uploads_path(session_key => cookies[session_key], request_forgery_protection_token => form_authenticity_token)
end

As you can see I have resource called uploads and I use the upload_path_with_session_information helper to give me a nice URL that when POSTED to from FLEX lets you keep your cookie store for everything else. 如您所见,我有一个称为上载的资源,并且我使用upload_path_with_session_information帮助程序为我提供了一个不错的URL,从FLEX发布到该URL后,您可以将Cookie存储为其他所有内容。

I honestly don't remember where I found this information online, but I can't take the credit for it. 老实说,我不记得我在网上找到这些信息的地方,但是我不能相信它。 Hope it makes sense for you. 希望这对您有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM