Read local files in flex [image as an example]

Source: Internet
Author: User

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/



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.