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();
}