MVC使用uploadify3.1 IE下正常 firefox、chrome出現HTTPERROR 302錯誤解決辦法


jquery uploadify在ie下可以正常上傳,在實現異步上傳的時候,每一個文件在上傳時都會提交給服務器一個請求。每個請求都需要安全驗證,session 和cookie的校驗。是的,就是這樣。由於jquery uploadify是借助flash來實現上傳的,每一次向后台發送數據流請求時,ie會自動把本地cookie存儲捆綁在一起發送給服務器。但 firefox、chrome不會這樣做,他們會認為這樣不安全。

首先需要對global.asxa添加如下內容

 

 1         protected void Application_BeginRequest(object sender, EventArgs e)
 2         {
 3             /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
 4             try
 5             {
 6                 string session_param_name = "ASPSESSID";
 7                 string session_cookie_name = "ASP.NET_SessionId";
 8 
 9                 if (HttpContext.Current.Request.Form[session_param_name] != null)
10                 {
11                     UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
12                 }
13                 else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
14                 {
15                     UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
16                 }
17             }
18             catch
19             {
20             }
21 
22             try
23             {
24                 string auth_param_name = "AUTHID";
25                 string auth_cookie_name = FormsAuthentication.FormsCookieName;
26 
27                 if (HttpContext.Current.Request.Form[auth_param_name] != null)
28                 {
29                     UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
30                 }
31                 else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
32                 {
33                     UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
34                 }
35 
36             }
37             catch
38             {
39             }
40         }
41 
42         private void UpdateCookie(string cookie_name, string cookie_value)
43         {
44             HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
45             if (null == cookie)
46             {
47                 cookie = new HttpCookie(cookie_name);
48             }
49             cookie.Value = cookie_value;
50             HttpContext.Current.Request.Cookies.Set(cookie);
51         }

 

初始化頁面上傳插件代碼如下

 1     <script type="text/javascript">
 2         var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)";
 3         var ASPSESSID = "@Session.SessionID";
 4 
 5         $(function () {
 6             $('#upload').uploadify({
 7                 'formData': { 'folder': '/Upload', 'ASPSESSID': ASPSESSID, 'AUTHID': auth },
 8                 'buttonText': '瀏覽',
 9                 'buttonClass': 'browser',
10                 'fileSizeLimit' : '100KB',
11                 'fileTypeExts': '*.xls;*.xlsx',
12                 'removeCompleted': false,
13                 'swf': '@Url.Content("~/Scripts/Uploadify/uploadify.swf")',
14                 'uploader': '/Upload',
15                 'onUploadSuccess': function (file, data, response) {}
16             });
17         });
18     </script>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM