2011-03-20 16 views
1

我正在使用uploadify上傳我的文件,並且我想要保存這些文件,並且希望將路徑保存在數據庫中,因此我在會話中以及用戶提交表單後保存路徑。它適用於Internet Explorer,但在Firefox上,由於會話Id的更改,它不起作用。爲什麼會話ID在Firefox中更改

如何解決這個問題?

+0

可能重複[會議和uploadify(HTTP: //stackoverflow.com/questions/1284666/sessions-and-uploadify) – 2011-03-20 08:17:49

+0

謝謝,但我不知道的PHP – Mazen 2011-03-20 08:41:03

回答

2

uploadify插件不發送cookie,因此服務器無法識別會話。一種可能的方式實現這一目標是使用scriptData參數包括SessionID的作爲請求參數:

<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" /> 
<% } %> 

這將文件沿着ASPSESSID參數添加到請求。接下來,我們需要重新構建服務器上的會話。這可能在Application_BeginRequest方法來完成在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); 
    } 
} 

,並最終將接收上傳的控制器動作可以使用會話:的

[HttpPost] 
public ActionResult Index(HttpPostedFileBase fileData) 
{ 
    // You could use the session here 
    var foo = Session["foo"] as string; 
    return View(); 
}