第九篇,仿URLLoader讀取檔案
先看看最後的程式碼
function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function readFileOk(){ mytxt.text = urlloader.data; }
基本上已經實作了Actionscript的模仿了。
效果和程式碼看這裡,看不到效果的請下載支援HTML5的瀏覽器
http://fsanguo.comoj.com/html5/jstoas09/index.html
其實說實作過程
其實說實作過程
if(!file_exists($_POST["file"])){ echo ""; exit; } $file = fopen($_POST["file"],"r"); $filemsg = ""; while (!feof($file)) { $line = fgets($file); $filemsg = $line; } fclose($file); echo $filemsg;
function LURLLoader(){ var self = this; self.objectindex = ++LGlobal.objectIndex; self.type="LURLLoader"; self.loadtype = ""; self.content = null; self.oncomplete = null; self.event = {}; } LURLLoader.prototype = { addEventListener:function(type,listener){ var self = this; if(type == LEvent.COMPLETE){ self.oncomplete = listener; } }, load:function (path,loadtype){ var self = this; self.loadtype = loadtype; if(self.loadtype == "text"){ $.post(LEGEND_FILE_PHP, { flg:"read", file:path },function(data){ if(self.oncomplete){ self.event.currentTarget = data; self.data = data; self.oncomplete(self.event); } }); } } }
init(40,"mylegend",600,500,main); var loadingLayer; var backLayer; var urlloader var mytxt; function main(){ legendLoadOver(); var readBtn = addButton("读取",20); readBtn.x = 10; readBtn.y = 20; addChild(readBtn); readBtn.addEventListener(LMouseEvent.MOUSE_DOWN, readFile); mytxt = new LTextField(); mytxt.x = 10; mytxt.y = 50; mytxt.text = ""; mytxt.width = 300; mytxt.height = 200; mytxt.setType(LTextFieldType.INPUT); addChild(mytxt); } function readFileOk(){ mytxt.text = urlloader.data; } function readFile(){ urlloader = new LURLLoader(); urlloader.addEventListener(LEvent.COMPLETE,readFileOk); urlloader.load("../file/test.txt","text"); } function addButton(lbl,x){ var up = new LSprite(); up.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#999999"); var txt = new LTextField(); txt.x = x; txt.text = lbl; up.addChild(txt); var over = new LSprite(); over.graphics.drawRect(1,"black",[0, 0, 80, 20],true,"#cccccc"); var txt1 = new LTextField(); txt1.x = x; txt1.text = lbl; over.addChild(txt1); var btn = new LButton(up,over); return btn; }