Tag Archives: flash

Uploadify session workaround

Due to design of Flash that Uploadify uses, there is a known problem whereby the Flash component of Uploadify does not pass back the cookies. So things like sessions do not work as expected out-of-the-box.

Luckily, there’s a workaround: uploadify has a scriptData option that you can populate from the session cookie  (perhaps using a jQuery cookie plugin for convenience). The option value is then passed to the backend app, which can use it to switch to the proper session. For example as follows:

First, a small function to fetch the session id:

var sid = function () {
   return {'sessionid':$.cookie('sessionid')}
};

Then, as part of the Uploadify initialization, set scriptData to pass the return value:

$(document).ready(function() {
   $('#file_upload').uploadify({
     'uploader'  : '/uploadify/uploadify.swf',
     'script'    : '/upload_files',
     'scriptData': sid(), // <--- our function
});

This is just an incomplete example; for the rest of the options you want to initialize Uploadify with, see Uploadify docs.
As for backend processing, scriptData comes in as regular POST variables. I was building a simple Django app when encountering this problem, so here’s what I did to fetch the session id and switch to another Django session using it.

# get session id that was passed in via scriptData
sid = request.POST['sessionid']

# Switch to another session by triggering the
# SessionStore session loading machinery, using
# the proper session id that we passed in from
# Uploadify.
session = request.session
session._set_session_key(sid)
session._get_session()

That’s all.

Advertisements