当前个人档案的头像面临访问量过大的问题,目前的解决方案是把上传和显示头像的功能独立出来成为一个新的站点并把此站点转放到另外一台的Web服务器上,以提高其响应能为。同时,因为原来的对头像的获取是通过aspx加载真实的头像(jpg)然后显示出来的,这样的处理方式在大访问量的情况之下对性能是有影响的,所以现在当独立到新的站点时就取消了这样的功能,把原来头像URL如:http://profile.csdn.net/billok/picture/1.jpg 转换为真实的访问地址"

    其中的类库的,只要引用一下md5.js就可以很方便的使用了,剩下的工作就是对Avatar的URL进行封装一下就可以了。

/*JS获取用户头像真实URL*/
var AvatarHost = "avatar.profile.csdn.net";

function Avatar1(userName) {
    
return GetAvatarUrl(AvatarHost, userName, 1false);
}

function Avatar2(userName) {
    
return GetAvatarUrl(AvatarHost, userName, 2false);
}

function Avatar3(userName) {
    
return GetAvatarUrl(AvatarHost, userName, 3false);
}

function Avatar4(userName) {
    
return GetAvatarUrl(AvatarHost, userName, 4false);
}

function AvatarICO(userName) {
    
return GetAvatarUrl(AvatarHost, userName, 1true);
}

function GetAvatarUrl(userName, type, getIcon) {
    
return GetAvatarUrl(AvatarHost, userName, type, getIcon);
}

function GetAvatarUrl(host, userName, type, getIcon) {
    
var url = "http://";
    
//添加域名
    url += host;
    
//添加MD5计算的路径
    url += MakeAvatarPath(userName);
    
//计算图片大小
    if (getIcon == true) {
        url 
+= "_";
    }
    
else {
        url 
+= type + "_";
    }
    
//添加用户名
    url += userName;
    
//添加图片类型
    if (getIcon == true) {
        url 
+= ".ico";
    }
    
else {
        url 
+= ".jpg";
    }

    
return url;
}
function MakeAvatarPath(userName) {
    
var hash = hex_md5(userName.toLowerCase()); //转成小写,并计算MD5值
    var hashArray = hash.split(""); //把字符串转为字符数组

    
var x = "/" + hashArray[0+ "/" + hashArray[1+ "/" + hashArray[2+ "/";
    
return x
}

 

调用过程也很简单,这就不多说了,看一下下面的调用例子:


    <title>JS的MD5</title>
    
<script type="text/javascript" src="js/md5.js"></script>
    
<script type="text/javascript" src="js/AvatarUrls.js"></script>
    
<script type="text/javascript">
        
//AvatarHost = "avatar2.profile.csdn.net";

        
function Button1_onclick() {
            AvatarHost 
= document.getElementById("txtHost").value;

            
var txtUserName = document.getElementById("txtUserName");
            
var userName = txtUserName.value;

            document.getElementById(
'avatar1').innerHTML = Avatar1(userName);
            document.getElementById(
'avatar1Img').src = Avatar1(userName);

            document.getElementById(
'avatar2').innerHTML = Avatar2(userName);
            document.getElementById(
'avatar2Img').src = Avatar2(userName);

            document.getElementById(
'avatar3').innerHTML = Avatar3(userName);
            document.getElementById(
'avatar3Img').src = Avatar3(userName);

            document.getElementById(
'avatar4').innerHTML = Avatar4(userName);
            document.getElementById(
'avatar4Img').src = Avatar4(userName);

            document.getElementById(
'avatarICO').innerHTML = AvatarICO(userName);
            document.getElementById(
'avatarICOImg').src = AvatarICO(userName);
        }
        

    
</script>
</head>
<body>
    Host: 
<input id="txtHost" type="text" value="avatar.profile.csdn.net" />
    UserName: 
<input id="txtUserName" type="text" value="billok" />
    
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
    
<p>
        
<img id="avatar1Img" src="" alt="" />
        
<span id="avatar1"></span>
    
</p>
    
<p>        
        
<img id="avatar2Img" src="" alt="" />
        
<span id="avatar2"></span>
    
</p>
    
<p>
         
<img id="avatar3Img" src="" alt="" />
        
<span id="avatar3"></span>
    
</p>
    
<p>
        
<img id="avatar4Img" src="" alt="" />
        
<span id="avatar4"></span>
    
</p>
    
<p>
         
<img id="avatarICOImg" src="" alt="" />
        
<span id="avatarICO"></span>
    
</p>
</body>
</html>

相关文章: