在一个Flash制作的图片上传程序中(使用了FileReference,但这玩意儿不能返回服务器数据),需要在as 中生成guid做为服务端文件的文件名,起初想使用js生成然后再传入flash,js生成方法如下:
<
SCRIPT
LANGUAGE
="JavaScript"
>
<!--
function guid()
{
var TypeLib = new ActiveXObject( "Scriptlet.TypeLib" );
strGUID = TypeLib.Guid;
alert (strGUID );
}
//-->
</ SCRIPT >
< a href ="javascript:void(0);guid()" > test </ a >
<!--
function guid()
{
var TypeLib = new ActiveXObject( "Scriptlet.TypeLib" );
strGUID = TypeLib.Guid;
alert (strGUID );
}
//-->
</ SCRIPT >
< a href ="javascript:void(0);guid()" > test </ a >
但是js生成时,浏览器会弹出ActiveX控件的交互询问,不爽,但此种方式应该是生成真正的guid。
后来想,使用as生成伪guid,应该可以,这种方法虽然理论上不是完全唯一,但是重复的几率已经很少了。
GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。例如:394ac5df-7259-2598-ccda-525D119C1b8b即为有效的 GUID 值,那么根据此规则,写出as的伪guid生成方法如下:
function
createUID():String
{
var uid:String = "";
var ALPHA_CHARS:String = "0123456789abcdef";
var i:Number;
var j:Number;
for (i=0; i<8; i++) {//先成成前8位
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));
}
for (i=0; i<3; i++) {//中间的三个4位16进制数
uid += "-";
for (j=0; j<4; j++) {
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));
}
}
uid += "-";
var time:Number = new Date().getTime();
uid += ("0000000"+time.toString(16).toUpperCase()).substr(-8);//取后边8位
for (i=0; i<4; i++) {
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));//再循环4次随机拿出4位
}
return uid;
}
var uid:String = "";
var ALPHA_CHARS:String = "0123456789abcdef";
var i:Number;
var j:Number;
for (i=0; i<8; i++) {//先成成前8位
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));
}
for (i=0; i<3; i++) {//中间的三个4位16进制数
uid += "-";
for (j=0; j<4; j++) {
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));
}
}
uid += "-";
var time:Number = new Date().getTime();
uid += ("0000000"+time.toString(16).toUpperCase()).substr(-8);//取后边8位
for (i=0; i<4; i++) {
uid += ALPHA_CHARS.charAt(Math.round(Math.random()*15));//再循环4次随机拿出4位
}
return uid;
}