Write HTML5 using the syntax similar to ActionScript-Article 9: Reading files using urlloader

Source: Internet
Author: User
Tags readfile

Article 9: copying urlloader to read files

First look at the final code

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.

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.

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:

Init (40, "mylegend", 600,500, main); var loadinglayer; var backlayer; var urlloadervar 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 = ""; 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 ;}

Over. It completes reading text files in imitation of ActionScript.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.