1

I am using uploadify to upload my files and I want to save the files and I want to save path in database, so I am saving the path in session and after the user submit the form. It works on Internet Explorer but on Firefox it's not working because of the change of the session Id.

How to solve this problem?

Mazen
  • 81
  • 1
  • 8
  • possible duplicate of [Sessions and uploadify](http://stackoverflow.com/questions/1284666/sessions-and-uploadify) – Keith Adler Mar 20 '11 at 08:17

1 Answers1

2

The uploadify plugin doesn't send cookies so the server cannot identify the session. One possible way to achieve this is to use the scriptData parameter to include the sessionId as request parameter:

<script type="text/javascript">
    $(function () {
        $('#file').uploadify({
            uploader: '<%= Url.Content("~/Scripts/jquery.uploadify-v2.1.4/uploadify.swf") %>',
            script: '<%= Url.Action("Index") %>',
            folder: '/uploads',
            scriptData: { ASPSESSID: '<%= Session.SessionID %>' },
            auto: true
        });
    });
</script>

<% using (Html.BeginForm()) { %>
    <input id="file" name="file" type="file" />
    <input type="submit" value="Upload" />
<% } %>

This will add the ASPSESSID parameter to the request along with the file. Next we need to reconstruct the session on the server. This could be done in the Application_BeginRequest method in Global.asax:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string sessionParamName = "ASPSESSID";
    string sessionCookieName = "ASP.NET_SessionId";

    if (HttpContext.Current.Request[sessionParamName] != null)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[sessionCookieName];
        if (null == cookie)
        {
            cookie = new HttpCookie(sessionCookieName);
        }
        cookie.Value = HttpContext.Current.Request[sessionParamName];
        HttpContext.Current.Request.Cookies.Set(cookie);
    }
}

and finally the controller action that will receive the upload could use the session:

[HttpPost]
public ActionResult Index(HttpPostedFileBase fileData)
{
    // You could use the session here
    var foo = Session["foo"] as string;
    return View();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928