Article 9: copying URLLoader to read files
First look at the final code
Www.2cto.com
Function readFile (){
Urlloader = new LURLLoader ();
Urlloader. addEventListener (LEvent. COMPLETE, readFileOk );
Urlloader. load ("../file/test.txt", "text ");
}
Function readFileOk (){
Mytxt. text = urlloader. data;
}
Basically, you have achieved the imitation of Actionscript.
For more information about the effects and codes, see <G id = "1"> html5 browsers </G>.
Http://fsanguo.comoj.com/html5/jstoas09/index.html
The following describes the implementation process.
In fact, ActiveXObject in javascript can be used to read and write local files, but the security level of your browser must be set to the lowest level, but the games and webpages we make must be put online, we cannot ask all users to do so.
Here, I use php to implement this process. php can freely read files on the server and does not rely on the settings of the user's browser.
It is easy to read files using php. You can use a fopen function. The following is the file. php code.
Www.2cto.com
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;
Put this php in your favorite location, and set the path LEGEND_FILE_PHP in legend. js to point to your location.
For javascript calls to php, you can write it by yourself, because it is not complicated, but I am a very lazy person, so I called it directly using jquery. What is jquery? I don't need to explain it.
The construction of LURLLoader is basically the same as that of LLoader. Only the load method is different. The following is the complete code of the LURLLoader class, which calls the prepared php to obtain the text to be read.
Www.2cto.com
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 );
}
});
}
}
}
For the above example, I added a button and an LTextField. The Code is as follows:
Www.2cto.com
Init (40, "mylegend", 600,500, main );
Var loadingLayer;
Var backLayer;
Var urlloader
Var mytxt;
Function main (){
LegendLoadOver ();
Var readBtn = addButton ("read", 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 = "";
MySQL. txt. 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;
}
Over. It completes reading text files in imitation of ActionScript.
From lufy hut