Flex 4 ~ upload image file and set image width, height

MXML
--------------------------------------------------------------------------------------------------------
<mx:Button label="Browse" click="Image_Browse();" />
<mx:Image id="PreviewImage" x="0" y="0" />
--------------------------------------------------------------------------------------------------------




Action Script
--------------------------------------------------------------------------------------------------------
private var ImageTypes:FileFilter = new FileFilter("Image (*.png, *.jpg, *.gif)", "*.png; *.jpg; *.gif");
private var UploadFilter:Array = new Array(ImageTypes);
private var FileRef:FileReference = new FileReference();

private function Image_Browse() : void {
FileRef.addEventListener(Event.SELECT, Image_Select);
FileRef.addEventListener(Event.COMPLETE, PreviewImageHandler);
FileRef.browse(UploadFilter);
}

private function Image_Select(e:Event) : void {
FileRef.load();
}

private function PreviewImageHandler(e:Event) : void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.loadBytes(FileRef.data);
PreviewImage.source = FileRef.data;
}

//set image width, height
public function loader_complete (event:Event) : void {
var sourceBMP:Bitmap = event.currentTarget.loader.content as Bitmap;
PreviewImage.width = sourceBMP.width;
PreviewImage.height = sourceBMP.height;
}


--------------------------------------------------------------------------------------------------------

reference
http://stackoverflow.com/questions/3439532/flex-4-filereference-selected-image-file-dimmensions-width-and-height

沒有留言:

發佈留言