Home >Web Front-end >H5 Tutorial >Use ActionScript-like syntax to write html5 - Part 9, imitating URLLoader to read files

Use ActionScript-like syntax to write html5 - Part 9, imitating URLLoader to read files

黄舟
黄舟Original
2017-01-17 17:00:171264browse

Part 9, imitating URLLoader to read files

Let’s take a look at the final code first

function readFile(){
urlloader = new LURLLoader();
urlloader.addEventListener(LEvent.COMPLETE,readFileOk);
urlloader.load("../file/test.txt","text");
}
function readFileOk(){
mytxt.text = urlloader.data;
}

Basically, the imitation of Actionscript has been realized.

Look here for the effect and code. If you cannot see the effect, please download a browser that supports HTML5

http://fsanguo.comoj.com/html5/jstoas09/index.html

Let’s talk about the implementation process
In fact, ActiveXObject in JavaScript can read and write local files, but the security level of your browser must be set to the lowest, but what we do Games and web pages must be put online, and there is no way we can require all users to do so.

Here, I use PHP to implement this process. PHP can freely read files on the server. It does not depend on the user's browser settings.

Read files with PHP It's very simple. It can be done with a fopen function. The following is the code of file.php

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 the location you like, and then set the path LEGEND_FILE_PHP in legend.js to point to the location you put

Regarding javascript calling php, of course you can write it yourself, because it is not complicated, but I am a very lazy person, so I directly use jQuery to call it. What is jquery? I guess I don’t need to explain it

Regarding the structure of LURLLoader, it is basically the same as LLoader, only the load method is different. The following is the complete code of the LURLLoader class, which calls the previously prepared php to obtain the text to be read.

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);
}
});
}
}
}

Regarding the above example, I added a button and a LTextField, the code is as follows

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;
}

The above is to use the syntax of imitating ActionScript to write html5 - Part 9, imitating URLLoader reading Get the content of the file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn