cookies的简单使用 客户端保存临时数据和上传文件的就Query.uploadify(2.xx版本)的使用

        <script>


        //删除cookie
        function delCookie(name){//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
           var date = new Date();
           date.setTime(date.getTime() - 10000);
           document.cookie = name + "=a; expires=" + date.toGMTString();
        }

        
        
        //获取coookie
        function getCookie(cookie_name)
        {
            var allcookies = document.cookie;
            var cookie_pos = allcookies.indexOf(cookie_name);
        
            // 如果找到了索引,就代表cookie存在,
            // 反之,就说明不存在。
            if (cookie_pos != -1)
            {
            // 把cookie_pos放在值的开始,只要给值加1即可。
            cookie_pos += cookie_name.length + 1;
            var cookie_end = allcookies.indexOf(";", cookie_pos);
        
            if (cookie_end == -1)
            {
            cookie_end = allcookies.length;
            }
        
            var value = unescape(allcookies.substring(cookie_pos, cookie_end));
            }
        
            return value;
        }
        
        
        
        //判断属于数组 inarray    
        function inarray(obj, arr) 
        {
            if(typeof obj == 'string') 
            {
               for(var i in arr) 
               {
                if(arr[i] == obj) 
                {
                  return true;
                }
               }
            }
            return false;
        }     
            
        </script>
        
    <script type="text/javascript">
        
        var cookieStr = ",";
        document.cookie = "cookStr="+cookieStr;
        
        $(document).ready(function()
        {
            $("#uploadify").uploadify({
                'uploader': 'uploadify.swf',
                'script': 'uploadify.php',
                'cancelImg': 'cancel.png',
                'folder': 'Uploads',
                'queueID': 'fileQueue',
                'auto': true,
                'multi': true,
                'onSelect': function(e, queueId, fileObj){
                    
                    var cookieArr= new Array();   
                    var coStr = getCookie('cookStr');
                    coArr=coStr.split(","); 
                    
                    if(inarray(fileObj.name, coArr)){
                        document.cookie = queueId+"=0";
                        onCancel(f);
                    }else{
                        coStr +=  ","+fileObj.name;
                        document.cookie = "cookStr="+coStr; 
                        document.cookie = queueId+"=1";
                    }
                },
                                
                'onComplete':function (e,q,f,data,d) 
                {    
                    
                    var i = getCookie(q);
                    
                    delCookie(q);
                    
                    if(parseInt(i)){
                        var name = f.name;
                        var image = "<li><img width='120px' height='70px' src='Uploads/"+f.name+"' /></li>";    
                        $("#i").append(image);                            
                    }else{
                        return false;
                    }                    
                }    
                            
            });
        });  
    </script>
posted @ 2012-07-27 10:54  lcuzhanglei  阅读(211)  评论(0编辑  收藏  举报