AS3 - Load and display image in flash from local drive


/ Published in: ActionScript 3
Save to your folder(s)

Use this class to create a simple upload-to-flash button. Select image from your local harddrive and upload it in flash, later you can get is as Bitmap() object
All images are stored in static array, so you can retrive it anywhere you want in your code!


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Sprite;
  6.  
  7. import flash.events.Event;
  8.  
  9. import flash.text.TextField;
  10. import flash.text.AntiAliasType;
  11. import flash.text.GridFitType;
  12. import flash.text.TextFieldAutoSize;
  13. import flash.text.TextFieldType;
  14. import flash.text.TextFormat;
  15. import flash.text.TextFormatAlign;
  16.  
  17. import flash.display.Loader;
  18. import flash.errors.IOError;
  19. import flash.events.ProgressEvent;
  20. import flash.display.LoaderInfo;
  21. import flash.display.DisplayObject;
  22. import flash.net.FileFilter;
  23. import flash.net.FileReference;
  24. import flash.net.FileReferenceList;
  25. import flash.net.URLVariables;
  26. import flash.net.URLLoader;
  27. import flash.net.URLRequest;
  28. import flash.net.URLRequestMethod;
  29. import flash.net.URLLoaderDataFormat;
  30. import flash.net.URLRequestHeader;
  31. import flash.events.Event;
  32. import flash.events.MouseEvent;
  33.  
  34. public class ImageUploader extends Sprite {
  35.  
  36. //public static const containing all loaded images
  37. public static var IMAGES:Array = [];
  38.  
  39. //some event constants
  40. public static const EVENT_IMAGE_LOADED:String = "eventImageLoaded";
  41.  
  42. //private vars
  43. private var bg:Sprite;
  44. private var uploadbar:Sprite;
  45. private var label:TextField;
  46.  
  47. //actual image
  48. private var _image:Bitmap;
  49. protected var fileRef:FileReference;
  50.  
  51. public function ImageUploader() {
  52.  
  53. /*
  54. Author: Chrysto Panayotov ( burnandbass [@] gmail [dot] com )
  55. All images are stored in static ImageUploader.IMAGES array
  56. you can listen for ImageUploader.EVENT_IMAGE_LOADED event - fired when the image is loaded
  57.  
  58. usage
  59.  
  60. var upload1:ImageUploader = new ImageUploader();
  61. upload1.x = 100;
  62. upload1.y = 20;
  63. addChild(upload1);
  64.  
  65. upload1.addEventListener(ImageUploader.EVENT_IMAGE_LOADED, function():void{
  66. var image:Bitmap = upload1.image;
  67. image.y = 70;
  68. image.x = 10;
  69. addChild(image);
  70. });
  71.  
  72. */
  73.  
  74. this.mouseChildren = false;
  75. this.buttonMode = true;
  76.  
  77. bg = new Sprite();
  78. bg.graphics.beginFill(0x00ffff,0.7);
  79. bg.graphics.drawRoundRect(0,0,170,40,10,10);
  80. bg.graphics.endFill();
  81. addChild(bg);
  82.  
  83. var _format:TextFormat
  84. _format = new TextFormat();
  85. _format.font = "Helvetica";
  86. _format.color = 0x0000;
  87. _format.size = 14;
  88. _format.align = TextFormatAlign.CENTER;
  89.  
  90. label = new TextField();
  91. label.embedFonts = false;
  92. label.antiAliasType = AntiAliasType.ADVANCED;
  93. label.autoSize = TextFieldAutoSize.CENTER;
  94. label.multiline = false;
  95. label.cacheAsBitmap = true;
  96. label.selectable = false;
  97. label.text = "upload image";
  98. label.defaultTextFormat = _format;
  99. label.setTextFormat(_format);
  100.  
  101. label.y = 5;
  102. label.width = this.width
  103.  
  104. addChild(label);
  105.  
  106. uploadbar = new Sprite();
  107. uploadbar.graphics.beginFill(0xffffff,1);
  108. uploadbar.graphics.drawRect(0,0,this.width - 30, 2);
  109. uploadbar.graphics.endFill();
  110. uploadbar.x = 15;
  111. uploadbar.y = 30;
  112. uploadbar.scaleX = 0;
  113. addChild(uploadbar);
  114.  
  115. fileRef = new FileReference();
  116. fileRef.addEventListener(Event.SELECT, onFileSelect);
  117.  
  118. this.addEventListener(MouseEvent.CLICK, chooseFile);
  119. }
  120.  
  121. private function chooseFile(event:MouseEvent):void{
  122. this.removeEventListener(MouseEvent.CLICK, chooseFile);
  123. label.text = "image uploading";
  124. fileRef.browse([new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png, *.JPG)", "*.jpg;*.jpeg;*.gif;*.png; *.JPG")]);
  125. }
  126.  
  127. protected function onFileSelect(event:Event):void {
  128. this.buttonMode = false;
  129. fileRef.removeEventListener(Event.SELECT, onFileSelect);
  130. fileRef.addEventListener(Event.COMPLETE, onFileLoad);
  131. fileRef.addEventListener(ProgressEvent.PROGRESS, reportProgress);
  132. fileRef.load();
  133. }
  134.  
  135. private function reportProgress(event:ProgressEvent):void{
  136. this.dispatchEvent(event.clone());
  137. if(event.bytesLoaded > 1){
  138. this.alpha = 0.9;
  139. }
  140. uploadbar.scaleX = (event.bytesLoaded / event.bytesTotal);
  141. }
  142.  
  143. protected function onFileLoad(event:Event):void {
  144. fileRef.removeEventListener(Event.COMPLETE, onFileLoad);
  145. var image:Loader = new Loader();
  146. image.loadBytes(fileRef.data);
  147. image.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageParse);
  148. }
  149.  
  150. protected function onImageParse(event:Event):void {
  151. label.text = "image uploaded";
  152. var content:DisplayObject = LoaderInfo(event.target).content;
  153. _image = Bitmap(content);
  154.  
  155. IMAGES.push(_image);
  156.  
  157. this.dispatchEvent(new Event(ImageUploader.EVENT_IMAGE_LOADED));
  158.  
  159. //addChild(_image);
  160. //_image.y = 100;
  161. }
  162.  
  163. //getters
  164.  
  165. public function get image():Bitmap{
  166. return _image;
  167. }
  168.  
  169. }//end
  170. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.