2012/05/21

Operations on Local Interactions of Flex

Flash is unable to interact with local programs before the release of Flash Player 10, for generally the data has to be sent to the background and then sent from the background to the foreground to be saved to perform some operations like image saving. There is no doubt that this method is of much trouble, involving writing a series of  F/B interactive processing. Fortunately, in Flash Player 10, Adobe has offered some interfaces to make the local interactions possible, such as local file saving, file opening and file uploading. Taking TWaver's FlexDemo as an example, on the toolbar, operations like saving images & xml and opening xml have been provided. You could see the following instance:


You could interact with local files conveniently by clicking these several buttons. Image saving and XML saving are fairly easy, just calling the "save"method of FileReference. XML opening calls the "browse"method of FileReference. After loading the data, you could read it in directly by var xmlText:String = fr.data.readUTFBytes(fr.data.length);.  The code is as follows:

var fr:FileReference = new FileReference();
if (fr.hasOwnProperty("browse")) {
  fr.addEventListener(Event.SELECT, function(e:Event):void {
    fr.load();
  });
  fr.addEventListener(Event.COMPLETE, function(ex:Event):void {
    var xmlText:String = fr.data.readUTFBytes(fr.data.length);
    var serializer:XMLSerializer = new XMLSerializer(network.elementBox);
    serializer.deserialize(xmlText,network.currentSubNetwork);
  });
  var fileFilter:FileFilter = new FileFilter("XML: (*.xml)", "*.xml");
  fr.browse([fileFilter]);
  } else {
    Alert.show("install Flash Player 10 to run this feature", "Not Supported");
  }

However, when many clients are trying to run Demo in FB and to save images, there will be an error message box reading,"Install Flash Player 10 to run this feature." The reason is that not only the client-side is required to  have Flash Player10, but the environment argument (while compiling) to be -target player=10.0.0.Besides,  there will also be an alert if you just call fr.load();in Flash Builder4 with SDK4.1. But this problem can be settled down by adding -target-player=10.0.0.

For more detailed information, please refer to exporting images in Flex.

No comments:

Post a Comment