How to load a user-selected file in flash
The FileReference class allows to easily load files selected by a user in Flash. This is how:
1.- Declare a FileReference object:
You need to have the same FileReference object during all the process. From the ActionScript 3.0 reference:
If the FileReference object goes out of scope, any transaction that is not yet completed on that object is canceled upon leaving the scope. Be sure that your FileReference object remains in scope for as long as the upload, download, load or save is expected to continue.
To solve it, declare the FileReference object in a place in which it remains in scope. Don't declare it in every function:
- ...
2.- Trigger the event:
Obviously, you need to use a button or something similar so that the user can choose when to load a file. When that event is triggered, this function should be called:
- {
- // The fileSelected function will be called when the users selects a file
- // The browse method receives an array of FileFilters
- }
3.- Start to load the file:
When the user chooses a file, the fileSelected function is called:
- {
- fileReference.load();
- }
4.- Get the contents of the file:
Finally, the fileLoaded function is called when the file is successfully loaded:
- {
- // fileReference.data contains the contents of the file as a ByteArray
- // Use the content of the file as you wish...
- }
The content of these loaded files is usually text or XML. There is other recipe that explains how to convert a ByteArray to text or XML.
Enviado por miguelSantirso hace about 1 month — modificado por última vez hace less than a minute