We often design a feature like this, such as changing the profile picture. This profile picture needs to be uploaded to the file system of the server, but the program wants to have a preview directly after the user chooses it, then the user uploads the file. Technically, this function requires reading local files. There is a class in Flash player10The filereference class can implement this function, and the interface for reading files is the load () function. Note that: A. This function can only be used in Ui operations, for example, you can press a button. B. After the local file is loaded, it cannot use C in as. This interface is an asynchronous process, so it is not loaded immediately. You need to add a listener for the operation. The following is the reference code
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: 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"
Creationcomplete = "creationcompletehandler (event)">
<FX: SCRIPT>
<! [CDATA [
Import flash.net. filereference;
Import flash.net. filefilter;
Import flash. Events. ioerrorevent;
Import flash. Events. event;
Private var fr: filereference;
Private var imagetypes: filefilter;
Private function creationcompletehandler (Event: Event): void {
Fr = new filereference ();
Imagetypes = new filefilter ("Images (*. jpg, *. JPEG, *. PNG, *. GIF )",
"*. Jpg; *. JPEG; *. PNG; *. gif ;")
// Add the listener after the user selects the file after opening the Browse File
Fr. addeventlistener (event. Select, selecthandler );
}
Private function browsehandler (Event: Event): void {
// Open the dialog of the browsing File
File. Browse ([imagetypes]);
}
Private function selecthandler (Event: Event): void {
// Add a listener after loading the file.
Fr. addeventlistener (event. Complete, onloadcomplete );
Fr. Load (); // load the selected file
}
Private function onloadcomplete (E: Event): void
{
Imgphoto. Source = file. Data;
}
]>
</FX: SCRIPT>
<Mx: Image id = "imgphoto" visible = "true" autoload = "true"
Width = "100" Height = "100"
Ioerror = "imageioerrorhandler (event)"/>
<Mx: button id = "btnbrowse" label = "Browse" Click = "browsehandler (event)"/>
The following is a reference article (the example of writing data to a local device in the article uses the Save Interface ):
Http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/