jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法
前段時(shí)間介紹過jquery uploadify上傳插件的使用方法,我在使用中遇到過Http Error 302錯(cuò)誤問題,應(yīng)該會(huì)有很多人在使用中遇到過,在此記錄下來:
首先http 302是請(qǐng)求被重定向的意思,這就很容易理解了,如果你的uploadify處理上傳腳本有session驗(yàn)證,就會(huì)出現(xiàn)此錯(cuò)誤,因?yàn)閒lash在執(zhí)行post請(qǐng)求的時(shí)候沒有包含cookie信息,而服務(wù)器的session會(huì)根據(jù)客戶端的cookie來得到SESSIONID。沒有提交cookie自然就不能獲取到session,然后uploadify就返回了302(請(qǐng)求被重定向)的錯(cuò)誤。
解決辦法當(dāng)然是把session_id的值傳到服務(wù)端:
<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>
然后在服務(wù)器端session驗(yàn)證之前:
if (isset($_POST['session'])){ session_id($_POST['session']); session_start();//注意此函數(shù)要在session_id之后 }
當(dāng)然,你也可以直接在url中將session id傳過去。
yii中代碼如下:
$('#<?php echo $upload_name_id;?>').uploadify({ 'buttonText': '選擇文件..', 'fileObjName': 'imgFile', 'method': 'post', 'multi': false, 'queueID': 'fileQueue', /*'uploadLimit': 2,*/ 'fileTypeExts': '*.gif;*.png;*.jpg;*.bmp;*.jpeg;', 'buttonImage': '<?php echo $this->_static_public?>/js/uploadify/select.png', 'formData': { 'sessionId' : '<?php echo Yii::app()->session->sessionID; ?>', 'timestamp' : '<?php echo time();?>', 'token' : '<?php echo md5('unique_salt'.time()); ?>', 'modelName' : '<?php echo $modelName; ?>', 'modelId' : '<?php echo $model->id; ?>' }, 'swf': '<?php echo $this->_static_public;?>/js/uploadify/uploadify.swf', 'uploader': '<?php echo $this->createUrl('uploadify/basicExecute')?>', 'onUploadStart': function () { $('#<?php echo $up_upload_name_id;?> img').remove(); $('#<?php echo $up_upload_name_id;?> a').remove(); $imgHtml = '<img class="upload_load" src="static/images/upload.gif" align="absmiddle" />'; $('#<?php echo $up_upload_name_id;?>').append($imgHtml); }, 'onUploadSuccess': function(file, data, response) { $('.upload_load').remove(); var json = $.parseJSON(data); if (json.state == 'success') { $("#<?php echo $d_upload_name_id;?>").remove(); $(yt_upload_name_id).val(json.fileId); $imgHtml ='<div id="<?php echo $d_upload_name_id;?>">'; $imgHtml += '<a href="<?php echo $this->_baseUrl?>/' + json.file + '" target="_blank">'; $imgHtml += '<img src="<?php echo $this->_baseUrl?>/'+json.file+'" width="85" height="75" align="absmiddle"/>'; $imgHtml += '</a>'; $imgHtml += '<a href="javascript:uploadifyRemove("' + json.fileId + '","<?php echo $d_upload_name_id;?>","<?php echo $yt_upload_name_id;?>")">刪除</a>'; $imgHtml +='</div>'; $('#<?php echo $up_upload_name_id;?>').append($imgHtml); } else { alert(json.message); } }, 'onQueueComplete':function () { $('.upload_load').remove(); } });
服務(wù)端:
if (isset($_POST['sessionId'])) { $session = Yii::app()->getSession(); $session->close(); $session->sessionID = $_POST['sessionId']; $session->open(); }
ps:jquery上傳插件uploadify使用心得(總結(jié))
自己使用實(shí)例:
1、jsp頁(yè)面:
<link href="jsp/js/jquery_upload/uploadify.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="jsp/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="jsp/js/jquery_upload/swfobject.js"></script> <script type="text/javascript" src="jsp/js/jquery_upload/jquery.uploadify.v2.1.4.min.js"></script> //jquery文件上傳 $(document).ready(function() { $("#uploadify").uploadify({ 'uploader': 'jsp/js/jquery_upload/uploadify.swf', 'script': 'uploadFile.svl', 'cancelImg': 'jsp/js/jquery_upload/cancel.png', 'queueID': 'fileQueue', 'auto': false, 'multi': true, 'method':'POST', 'scriptData':{'saveFolder':'stuPhotos'},//GET方式才可生效 'fileExt' :'*.jpg;*.gif;*.png', //控制可上傳文件的擴(kuò)展名 'fileDesc': 'jpg、gif、png文件', //控制可上傳文件的擴(kuò)展名描述,兩者需要同時(shí)使用 'buttonImg':'jsp/js/jquery_upload/selectBtn.gif', 'width':80,//"瀏覽"按鈕寬度 'onComplete':function(event,ID,fileObj,response,data){ //alert(response) //response為服務(wù)器響應(yīng)數(shù)據(jù) }, }); }); <td width="200" class="tabIndex" style="height:10px">照片:</td> <td> <input type="file" name="uploadify" id="uploadify" /> <p> <a href="javascript:$('#uploadify').uploadifyUpload()">上傳</a>| <a href="javascript:$('#uploadify').uploadifyClearQueue()">取消上傳</a> </p> <div id="fileQueue" ></div> <input type="hidden" name="stuPhoto" id="stuPhoto" /> </td>
2、服務(wù)端代碼
public class UploadFileUtil extends HttpServlet { private static final long serialVersionUID = 1L; File tmpDir = null;// 初始化上傳文件的臨時(shí)存放目錄 File saveDir = null;// 初始化上傳文件后的保存目錄 public UploadFileUtil() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ if(ServletFileUpload.isMultipartContent(request)){ response.setCharacterEncoding("utf-8");//務(wù)必,防止返回文件名是亂碼 DiskFileItemFactory dff = new DiskFileItemFactory();//創(chuàng)建該對(duì)象 dff.setRepository(tmpDir);//指定上傳文件的臨時(shí)目錄 dff.setSizeThreshold(1024000);//指定在內(nèi)存中緩存數(shù)據(jù)大小,單位為byte ServletFileUpload sfu = new ServletFileUpload(dff);//創(chuàng)建該對(duì)象 sfu.setFileSizeMax(5000000);//指定單個(gè)上傳文件的最大尺寸 sfu.setSizeMax(10000000);//指定一次上傳多個(gè)文件的總尺寸 FileItemIterator fii = sfu.getItemIterator(request);//解析request 請(qǐng)求,并返回FileItemIterator集合 while(fii.hasNext()){ FileItemStream fis = fii.next();//從集合中獲得一個(gè)文件流 if(!fis.isFormField() && fis.getName().length()>0){//過濾掉表單中非文件域 String fileName = fis.getName();//獲取文件名 String extName = ""; if (fileName.lastIndexOf(".") >= 0) { extName = fileName.substring(fileName.lastIndexOf(".")); } BufferedInputStream in = new BufferedInputStream(fis.openStream());//獲得文件輸入流 String uuidName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();//用UUID生成文件名 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"/"+uuidName+extName)));//獲得文件輸出流 Streams.copy(in, out, true);//開始把文件寫到你指定的上傳文件夾 } } //jquery上傳方式返回 response.getWriter().print("upload success");//成功 } }catch(Exception e){ response.getWriter().print("upload fail");//失敗 e.printStackTrace(); } } public void init() throws ServletException { super.init(); String serverPath = this.getServletConfig().getServletContext().getRealPath("/");//獲取服務(wù)器路徑 String tmpPath = serverPath+"/tmpUploadsFolder/"; String savePath = serverPath+"/uploadsFolder/"; tmpDir = new File(tmpPath); saveDir = new File(savePath); if(!tmpDir.isDirectory()) tmpDir.mkdir(); if(!saveDir.isDirectory()) saveDir.mkdir(); }}
以上內(nèi)容是小編給大家介紹的jQuery Uploadify 上傳插件出現(xiàn)Http Error 302 錯(cuò)誤的解決辦法,希望大家喜歡。
相關(guān)文章
jQuery Ajax 實(shí)現(xiàn)在html頁(yè)面實(shí)時(shí)顯示用戶登錄狀態(tài)
本文給大家分享jQuery Ajax 實(shí)現(xiàn)在html頁(yè)面實(shí)時(shí)顯示用戶登錄狀態(tài)的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12基于jquery實(shí)現(xiàn)可定制的web在線富文本編輯器附源碼下載
UEditor的功能非常強(qiáng)大,官方已配備了php,asp,asp.net,java等語(yǔ)言的上傳程序,拿來就可以使用,當(dāng)然ueditor更具有功能插件接口,很輕松地添加自己定義功能到編輯器中,方便項(xiàng)目的不同需求2015-11-11jQuery zTree如何改變指定節(jié)點(diǎn)文本樣式
這篇文章主要介紹了jQuery zTree如何改變指定節(jié)點(diǎn)文本樣式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10