Flex _ load data from a file;

Source: Internet
Author: User

Result output:

=> File_data: I love you, baby! I miss you so much! => File was successfully loaded; => loaded (33) of (33) bytes; => loaded (0) of (33) bytes;


=> Filereferenceloadexample

Package com. Learn. Mix
{
Import flash. Events. event;
Import flash. Events. ioerrorevent;
Import flash. Events. progressevent;
Import flash. Events. securityerrorevent;
Import flash.net. filefilter;
Import flash.net. filereference;
Import flash. utils. bytearray;

Import MX. Core. uicomponent;

/**
* Load data classes from files
* @ Author CEN
*/
Public class filereferenceloadexample extends uicomponent
{

/**
* Attribute */
/* File object */
Private var fileref: filereference;

/**
* Constructor
*/
Public Function filereferenceloadexample ()
{
/**
* Initialize the file object */
Fileref = new filereference ();
Fileref. addeventlistener (event. Select, onfileselectedhandler );
Fileref. addeventlistener (event. Cancel, oncancelhandler );
Fileref. addeventlistener (ioerrorevent. io_error, onioerrorhandler );
Fileref. addeventlistener (securityerrorevent. security_error, onsecurityerrorhandler );

/* Allowed file types */
VaR texttypefilter: filefilter = new filefilter ("textfiles (*. txt, *. rtf)", "*. txt; *. rtf ");

/* Select a file */
Fileref. Browse ([texttypefilter]);
}


/**
* Select a file
* @ Param event
*/
Private function onfileselectedhandler (Event: Event): void {
Fileref. addeventlistener (progressevent. Progress, onprogresshandler );
Fileref. addeventlistener (event. Complete, oncompletehandler );

/* Load data */
Fileref. Load ();
}

/**
* Loading progress
* @ Param event
*
*/
Private function onprogresshandler (Event: progressevent): void
{
Trace ("=> loaded (" + event. bytesloaded + ") of (" + event. bytestotal + ") bytes ;");
}

/**
* File loaded
* @ Param event
*
*/
Private function oncompletehandler (Event: Event): void
{
Trace ("=> file was successfully loaded ;");

VaR data: bytearray = fileref. Data;
Data. Position = 0;

// Var result: String = data. readutfbytes (data. Length); // if the external TXT file contains Chinese characters, garbled characters may occur. Therefore, use the following method for conversion;

VaR result: String = data. readmultibyte (data. length, "gb2312 ");
Trace ("=> file_data:" + result );
}

/**
* Cancel an event
* @ Param event
*
*/
Private function oncancelhandler (Event: Event): void
{
Trace ("=> the Browse request was canceled by the user ;");
}

/**
* Input/output errors
* @ Param event
*
*/
Private function onioerrorhandler (Event: ioerrorevent): void
{
Trace ("=> there was an IO error ;");
}

/**
* Security error
* @ Param event
*
*/
Private function onsecurityerrorhandler (Event: securityerrorevent): void
{
Trace ("=> there was a security error ;");
}
}
}

=> App. mxml

<? XML version = "1.0" encoding = "UTF-8"?>
<S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009"
Xmlns: S = "Library: // ns.adobe.com/flex/spark"
Xmlns: MX = "Library: // ns.adobe.com/flex/mx" minwidth = "955" minheight = "600"
Pagetitle = "thestudioofcenyebao" creationcomplete = "creationcompletehandler (event)">
<FX: SCRIPT>
<! [CDATA [
Import com. Learn. Mix. filereferenceloadexample;

Import MX. Events. flexevent;

Private var fileref: filereferenceloadexample;

Protected function creationcompletehandler (Event: flexevent): void
{

}

Protected function btn_clickhandler (Event: mouseevent): void
{
Fileref = new filereferenceloadexample ();
Addelement (fileref );
}

]>
</FX: SCRIPT>

<S: vgroup width = "450" Height = "230" verticalcenter = "0" horizontalcenter = "0" horizontalalign = "center" verticalalign = "Middle">
<S: button label = "loading data" Click = "btn_clickhandler (event)"/>
</S: vgroup>
</S: Application>

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.