Thursday, 20 January 2011

Trouble with Darren Johnstone asp.net file upload module

I'd used Darren Johnstone's brilliant file upload module before now, but I thought I'd take a look at SWFUpload just to see what it was like. I found SWFUpload a little too unintuitive to implement, so given my shortage of time I resorted back to the old faithful aforementiond DJFileUpload.

But this time round I just couldn't get it to work. I downloaded the sample C# project and that worked. I drafted all the relevant code into my own project but it still wouldn't work. I spent a long time trying to work out what was different between my project and the sample that worked. Was it because I was using VS2010 with .Net 4.0? What about some kind of permissions problem on my project?

Eventually I found the problem. While trying out SWFUpload I'd added the following code to my Global.asax file. This same code makes Darren Johnstone's file uploader no longer work! Remove it, and hey presto:

void Application_BeginRequest(object sender, EventArgs e)
        {
            /* Fix for the Flash Player Cookie bug in Non-IE browsers.
             * Since Flash Player always sends the IE cookies even in FireFox
             * we have to bypass the cookies by sending the values as part of the POST or GET
             * and overwrite the cookies with the passed in values.
             * 
             * The theory is that at this point (BeginRequest) the cookies have not been read by
             * the Session and Authentication logic and if we update the cookies here we'll get our
             * Session and Authentication restored correctly
             */

            try
            {
                string session_param_name = "ASPSESSID";
                string session_cookie_name = "ASP.NET_SESSIONID";

                if (HttpContext.Current.Request.Form[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
                }
            }
            catch (Exception)
            {
                Response.StatusCode = 500;
                Response.Write("Error Initializing Session");
            }

            try
            {
                string auth_param_name = "AUTHID";
                string auth_cookie_name = FormsAuthentication.FormsCookieName;

                if (HttpContext.Current.Request.Form[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
                }

            }
            catch (Exception)
            {
                Response.StatusCode = 500;
                Response.Write("Error Initializing Forms Authentication");
            }
        }

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!