1 Reply Latest reply on Jun 2, 2009 6:14 AM by Michael Borbor

    Not able to get image width after uploading image

    spugalia

      hi

       

      I am working on uploading image. Once the image is uploaded I need to know image width and height. Right now I am listening to "complete" event but it seems image is still not loaded when the complete event is raised as it returns image width as zero. This is very urgent and imp for me, Please let me know how can I get the wridth and height of image once it is uploaded on remote server

       

       

      <mx:VBox width="100%" height="100%">
                  <mx:Label text="Image" />
                  <mx:HBox width="100%" height="10%">
                      <mx:TextInput id="txt_imgname" width="321"/>
                      <mx:Button label="Browse"  click="bn_Browse_Click()"/>
                  </mx:HBox>
              <mx:Canvas width="400"  height="264" backgroundColor="#000000" borderStyle="solid" cornerRadius="12" borderThickness="3" borderColor="#222F92">
                  <mx:ProgressBar id="progressBar"  indeterminate="true" visible="false" height="1%"  />
                  <mx:Image id="img" complete="{imageLoadListener(event)}" />
              </mx:Canvas>

       

              <mx:HBox width="100%" height="10%" horizontalAlign="right" verticalAlign="middle">
                  <mx:Button label="OK" id="okbutton" click="bn_ok_click()" />
                  <mx:Button label="Cancel" click="bn_cancel_click()" />
              </mx:HBox>

       

       

      private function bn_Browse_Click():void
              {
                  CreateFileReference();
                   fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData);
                   FileBrowse();
              }
              private function FileBrowse():void
              {
                  try
                  {
                      var success:Boolean = fileReference.browse(allTypes);
                  }
                  catch (error:Error)
                  {
                      Alert.show("Unable to browse for files.");
                  }
              }
             
             
              private function CreateFileReference():void
              {
                   var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
                   allTypes = new Array(imageTypes);
                   fileReference = new FileReference();
                   fileReference.addEventListener(Event.SELECT, selectHandler);            
                   fileReference.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
              }
             
               private function selectHandler(event:Event):void
              {   
                   var request:URLRequest = new URLRequest("http://"+TestLoginIntegration.GetServerAddress()+"/FileUpload/Default.aspx");
                  try
                  {       
                       request.contentType = "application/octet-stream";
                       request.method = "POST";
                     fileReference.upload(request,fileReference.name,false);  
                  }
                  catch (error:Error)
                  {
                      Alert.show("Unable to upload file." + error.message);
                  }       
              }
             
             
               private function fileRef_progress(evt:ProgressEvent):void
                  {
                          img.source = "";
                      progressBar.visible = true;
                }
               
              private function onUploadCompleteData (event : DataEvent) : void
              {           
                   var filePath:String = event.data;
                   img.source = filePath;
                   progressBar.visible = false;
                   txt_imgname.text = GetFileNameFromPath(filePath);

       


              }
              private function imageLoadListener(e:Event):void{
                
      Alert.show(e.currentTarget.width);
              }

       

      private function GetFileNameFromPath(filepath:String):String
              {
                  if(filepath == null || filepath == "")
                  {
                      return null;
                  }
                  var name:String = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length);
                  return name;
              }