flash中读取数据库并显示在FLASH中的实例(通过ASP)
最近做项目,要求是用FLASH做一个公告发布,读取数据库里的内容并显示在FLASH里面.直接进入主题,讲讲我是怎么实现的.
只是提供一种思路,可能不是最好的
在flash中的代码为:
Flash中loadVars()作用是flash与服务器间传递参数
而sendAndLoad方法的用法是:
其中url是指要上传的变数的URL,若此FLASH在网页中执行,则URL必须和SWF档案位于相同的网域中.
targetObject是指接收下载变量的LOADVARS物件
METHOD是指HTTP通讯协议的GET或POST方法.
sendAndLoad()方法后,需要代码执行返回的结果,FLASH代码如下:
下面看看testFlash.asp的代码.
Flash传递过来的参数可在ASP里面直接用REQUEST来获得.
最后要注意的是上面代码最后一句话,都用是&变量来传递的,其它格式就是地址栏里面带的参数方式一样
只是提供一种思路,可能不是最好的
在flash中的代码为:
stop();
System.useCodepage = true;
var myVars = new LoadVars();
var i;
i = 1;
var counts;
counts = 4;
_global.heights ;
heights= 60;
myVars.state="no";
myVars.act="begin";
myVars.kinds = "IBM";
myVars.sendAndLoad("testFlash.asp", myVars, "post");
System.useCodepage = true;
var myVars = new LoadVars();
var i;
i = 1;
var counts;
counts = 4;
_global.heights ;
heights= 60;
myVars.state="no";
myVars.act="begin";
myVars.kinds = "IBM";
myVars.sendAndLoad("testFlash.asp", myVars, "post");
Flash中loadVars()作用是flash与服务器间传递参数
而sendAndLoad方法的用法是:
my_lv
.sendAndLoad(
url
, targetObject
[,
method
])
其中url是指要上传的变数的URL,若此FLASH在网页中执行,则URL必须和SWF档案位于相同的网域中.
targetObject是指接收下载变量的LOADVARS物件
METHOD是指HTTP通讯协议的GET或POST方法.
sendAndLoad()方法后,需要代码执行返回的结果,FLASH代码如下:
_root.onEnterFrame = function()
{
if(myVars.state=="ready")
{
x = 28;
y = 60;
if (Number(i) <= Number(counts))
{
//标题
if(i==1){
_root.createTextField("textBox" add i,(i*2)-1,x,y+ (1*i-1)*1.5,80,20);
}
else{
_root.createTextField("textBox" add i,(i*2)-1,x,heights+ (1*i-1),80,20);
}
eval("textBox" add i).type = "dynamic";
eval("textBox" add i).html = true;
eval("textBox" add i).text = eval("_root.myVars.title" add i);
eval("textBox" add i).textColor = 0xFB7509;
eval("textBox" add i).size = 8;
eval("textBox" add i).border = false;
eval("textBox" add i).multiline = false;
eval("textBox" add i).wordWrap = false;
eval("textBox" add i).autoSize = ture;
heights = heights + eval("textBox" add i)._height;
//內容
_root.createTextField("textBoxs" add i,(i*2),x+4,heights+(1*i-1),160,20);
eval("textBoxs" add i).html = true;
eval("textBoxs" add i).htmlText = eval("_root.myVars.content" add i);
eval("textBoxs" add i).textColor = 0x216EFA;
eval("textBoxs" add i).size = 8;
eval("textBoxs" add i).border = false;
eval("textBoxs" add i).multiline = true;
eval("textBoxs" add i).wordWrap = true;
eval("textBoxs" add i).autoSize = true;
heights = heights + eval("textBoxs" add i)._height;
i = Number(i) + 1;
_root.oknum=i;
}
}
}
创建的是循环产生4个标题和内容,主要是通过_root.myVars.XXX来获得传回的参数的值.{
if(myVars.state=="ready")
{
x = 28;
y = 60;
if (Number(i) <= Number(counts))
{
//标题
if(i==1){
_root.createTextField("textBox" add i,(i*2)-1,x,y+ (1*i-1)*1.5,80,20);
}
else{
_root.createTextField("textBox" add i,(i*2)-1,x,heights+ (1*i-1),80,20);
}
eval("textBox" add i).type = "dynamic";
eval("textBox" add i).html = true;
eval("textBox" add i).text = eval("_root.myVars.title" add i);
eval("textBox" add i).textColor = 0xFB7509;
eval("textBox" add i).size = 8;
eval("textBox" add i).border = false;
eval("textBox" add i).multiline = false;
eval("textBox" add i).wordWrap = false;
eval("textBox" add i).autoSize = ture;
heights = heights + eval("textBox" add i)._height;
//內容
_root.createTextField("textBoxs" add i,(i*2),x+4,heights+(1*i-1),160,20);
eval("textBoxs" add i).html = true;
eval("textBoxs" add i).htmlText = eval("_root.myVars.content" add i);
eval("textBoxs" add i).textColor = 0x216EFA;
eval("textBoxs" add i).size = 8;
eval("textBoxs" add i).border = false;
eval("textBoxs" add i).multiline = true;
eval("textBoxs" add i).wordWrap = true;
eval("textBoxs" add i).autoSize = true;
heights = heights + eval("textBoxs" add i)._height;
i = Number(i) + 1;
_root.oknum=i;
}
}
}
下面看看testFlash.asp的代码.
<!--#include file="conn.asp"-->
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<%
act=request("act")
if act="begin" then
set rs=server.CreateObject("adodb.recordset")
sql="SELECT * FROM WEBPORTAL_NEWS WHERE NewsScope = 'PCBABU' AND NewsLevel = '很重要' ORDER BY NewsSort ASC"
rs.open sql,conn,1,1
rs.movefirst()
str=""
i=1
counts = 0
do while not rs.eof
str= str & "&title" & i & "=" & rs("NewsClass")
str= str & "&content" & i & "=" & rs("NewsContents")
rs.movenext()
i=i+1
loop
counts = i - 1
rs.close()
set rs=nothing
conn.close()
set conn=nothing
end if
response.Write str & "&state=ready&counts=" & counts
%>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<%
act=request("act")
if act="begin" then
set rs=server.CreateObject("adodb.recordset")
sql="SELECT * FROM WEBPORTAL_NEWS WHERE NewsScope = 'PCBABU' AND NewsLevel = '很重要' ORDER BY NewsSort ASC"
rs.open sql,conn,1,1
rs.movefirst()
str=""
i=1
counts = 0
do while not rs.eof
str= str & "&title" & i & "=" & rs("NewsClass")
str= str & "&content" & i & "=" & rs("NewsContents")
rs.movenext()
i=i+1
loop
counts = i - 1
rs.close()
set rs=nothing
conn.close()
set conn=nothing
end if
response.Write str & "&state=ready&counts=" & counts
%>
Flash传递过来的参数可在ASP里面直接用REQUEST来获得.
最后要注意的是上面代码最后一句话,都用是&变量来传递的,其它格式就是地址栏里面带的参数方式一样