当前个人档案的头像面临访问量过大的问题,目前的解决方案是把上传和显示头像的功能独立出来成为一个新的站点并把此站点转放到另外一台的Web服务器上,以提高其响应能为。同时,因为原来的对头像的获取是通过aspx加载真实的头像(jpg)然后显示出来的,这样的处理方式在大访问量的情况之下对性能是有影响的,所以现在当独立到新的站点时就取消了这样的功能,把原来头像URL如:http://profile.csdn.net/billok/picture/1.jpg 转换为真实的访问地址"http://avatar.profile.csdn.net/1/c/5/1_billok.jpg"
其中的“/1/c/5/"是对用户名进行MD5生成的,在asp.net中这个很简单,不过对于一些纯用JS生成的URL就需要使用JS进行MD5运算了。提取JS的MD5,是有现成的类库的,只要引用一下md5.js就可以很方便的使用了,剩下的工作就是对Avatar的URL进行封装一下就可以了。
/*
JS获取用户头像真实URL
*/
var AvatarHost = " avatar.profile.csdn.net " ;
function Avatar1(userName) {
return GetAvatarUrl(AvatarHost, userName, 1 , false );
}
function Avatar2(userName) {
return GetAvatarUrl(AvatarHost, userName, 2 , false );
}
function Avatar3(userName) {
return GetAvatarUrl(AvatarHost, userName, 3 , false );
}
function Avatar4(userName) {
return GetAvatarUrl(AvatarHost, userName, 4 , false );
}
function AvatarICO(userName) {
return GetAvatarUrl(AvatarHost, userName, 1 , true );
}
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
}
var AvatarHost = " avatar.profile.csdn.net " ;
function Avatar1(userName) {
return GetAvatarUrl(AvatarHost, userName, 1 , false );
}
function Avatar2(userName) {
return GetAvatarUrl(AvatarHost, userName, 2 , false );
}
function Avatar3(userName) {
return GetAvatarUrl(AvatarHost, userName, 3 , false );
}
function Avatar4(userName) {
return GetAvatarUrl(AvatarHost, userName, 4 , false );
}
function AvatarICO(userName) {
return GetAvatarUrl(AvatarHost, userName, 1 , true );
}
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
}
调用过程也很简单,这就不多说了,看一下下面的调用例子:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<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>