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>