You want to load a local file to a byte array, without first uploading the file to the server then having to download it back as you have to in Flash Player 9.
Use the new load() method and data property in the FileReference API of Flash Player 10.
First, make sure your project is configured to use the Flash Player 10 playerglobal.swc in your Project Properties > Flex Build Path > Library Path. Remove the current playerglobal.swc under Flex 3.x and click Add Swc to add the Flash Player 10 version under
sdk_install_path/frameworks/libs/player/10. Now, under playerglobal.swc, double-click on Link Type and select External. Also, in Project Properties under the Flex Compiler section, enter 10.0.0 in the Require Flash Player version fields.
In the following implementation, we declare a new FileReference object, add listeners to it for the SELECT, IO_ERROR, PROGRESS and COMPLETE events then call the browse method. Assign the browseFileSystem() method as the handler for a button. When the user browses the file
system then selects a file, the selectHandler() method is called. This method loads the file using the new load() method on the FileReference instance. When loading is complete, the completeHandler() is called and you can access the byte array with the data from the local file using the new
property data on the FileReference instance. Now, you can either directly work on the file (e.g a bitmap editing tool) or upload the file to the server using RemoteObject (as opposed to using a server side script with FileReference.upload()), which might better fit into your existing software
architecture (in the case all your communication is done with RemoteObject via BlazeDS / LCDS and you would like to write files as BLOBs to your database). Finally, there is a bug in Flash Player that prevents it from uploading files via FileReference through HTTPS if you use Firefox*. Uploading a
file as a ByteArray via a RemoteObject and SecureHTTPChannel is a way of bypassing this problem.
import flash.net.FileReference;
import mx.controls.Alert;
public function browseFileSystem():void {
var file:FileReference = new FileReference();
file.addEventListener(Event.SELECT, selectHandler);
file.addEventListener(IOErrorEvent.IO_ERROR,
ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS,
progressHandler);
file.addEventListener(Event.COMPLETE,
completeHandler);
file.browse();
}
private function selectHandler(event:Event):void {
var file:FileReference =
FileReference(event.target);
file.load();
}
private function
ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function
progressHandler(event:ProgressEvent):void {
var file:FileReference =
FileReference(event.target);
trace("progressHandler: name=" + file.name + "
bytesLoaded=" + event.bytesLoaded + " bytesTotal=" +
event.bytesTotal);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
Alert.show((file.data is ByteArray).toString());
}
NB: the antagonistic method also exists in the updated API: fileReferenceInstance.save(byteArray, "defaultFileName"); saves a byte array to the local file system.
(*) Firefox HTTPS FileReference upload issue
https://bugs.adobe.com/jira/browse/FP-201
+