之前介紹過jquery uploadify上傳插件的使用方法,我在使用中遇到過Http Error 302錯誤問題,應該會有很多人在使用中遇到過,在此記錄下來:
首先http 302是請求被重定向的意思,這就很容易理解了,如果你的uploadify處理上傳腳本有session驗證,就會出現此錯誤,因為flash在執行post請求的時候沒有包含cookie信息,而服務器的session會根據客戶端的cookie來得到SESSIONID。沒有提交cookie自然就不能獲取到session,然后uploadify就返回了302(請求被重定向)的錯誤。
解決辦法:
把session_id的值傳到服務端:
<script> $(document).ready(function() { $('#file_upload').uploadify({ 'uploader' : 'uploadify/uploadify.swf', 'script' : 'uploadify.php', 'folder' : 'uploads/file', 'formData': { 'session': '<?php echo session_id();?>'}, 'onComplete' : function(event, ID, fileObj, response, data) { alert(response); } }); }); </script>
然后在服務器端session驗證之前:
if (isset($_POST['session'])){ session_id($_POST['session']); session_start();//注意此函數要在session_id之后 }
當然,你也可以直接在url中將session id傳過去,這樣Http Error 302錯誤就可以得到解決。
問題擴展:MVC使用uploadify3.1 IE下正常firefox、chrome也出現HTTPERROR 302錯誤,有什么解決辦法?
jquery uploadify在ie下可以正常上傳,在實現異步上傳的時候,每一個文件在上傳時都會提交給服務器一個請求。每個請求都需要安全驗證,session 和cookie的校驗。是的,就是這樣。由于jquery uploadify是借助flash來實現上傳的,每一次向后臺發送數據流請求時,ie會自動把本地cookie存儲捆綁在一起發送給服務器。但 firefox、chrome不會這樣做,他們會認為這樣不安全。
首先需要對global.asxa添加如下內容
protected void Application_BeginRequest(object sender, EventArgs e) { /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ 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 { } 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 { } } private void UpdateCookie(string cookie_name, string cookie_value) { HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); if (null == cookie) { cookie = new HttpCookie(cookie_name); } cookie.Value = cookie_value; HttpContext.Current.Request.Cookies.Set(cookie); }
初始化頁面上傳插件代碼如下
<script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)"; var ASPSESSID = "@Session.SessionID"; $(function () { $('#upload').uploadify({ 'formData': { 'folder': '/Upload', 'ASPSESSID': ASPSESSID, 'AUTHID': auth }, 'buttonText': '瀏覽', 'buttonClass': 'browser', 'fileSizeLimit' : '100KB', 'fileTypeExts': '*.xls;*.xlsx', 'removeCompleted': false, 'swf': '@Url.Content("~/Scripts/Uploadify/uploadify.swf")', 'uploader': '/Upload', 'onUploadSuccess': function (file, data, response) {} }); }); </script>
更多精彩內容請參考專題《ajax上傳技術匯總》,《javascript文件上傳操作匯總》和《jQuery上傳操作匯總》進行學習。
一個問題的研究可以是發散性的是多方面,我們要學會舉一反三,這樣才能靈活的學習專業知識,掌握專業技能,希望對大家的學習有所幫助。