Archive

Archive for April, 2008

Validation for stopping wrong file Upload at Browsing time

April 28, 2008 shashankkulkarni 3 comments

Hi,

We all know that to make use of browse window facility, Flex provides inbuilt class ‘FileReference’ which has method named “browse”, Which allows us to open the File Browsing window.

To restrict the Browsing window to open some limited file extensions(Such as .mp3,.mp4 file only), we make use of  FileFilter class which holds the fileFilter to be applied to the browsing window

eg.

private var allFileFilter:FileFilter = new FileFilter(“All, *.mp3; *.mp4; *.flv; *.wmv; *.mpeg; *.mpg;”, “*.mp3; *.mp4; *.flv; *.wmv; *.mpeg; *.mpg; “);

private var fileRef:FileReference = new FileReference();
fileRef.browse([allFileFilter]) ;

But this File filter validation can be broken easily to Upload wrong files, For this just type *.* and press enter , Here you see all files available for upload Now select Any Invalid file and go with upload.

So in such cases we need to apply self made file extesion validation logic which should be able to verify the extension of the file to be uploaded.

So here is short and sweet sample code to workout this logic,

Best Luck :)

 

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml“  pageTitle=”Upload Functionality Usage And Validation” layout=”absolute”>
 <mx:Script>
  <![CDATA[
   import mx.utils.StringUtil;

   
   /**
   * Standard File extensions allowed to be Uploaded
   */
   private var fileTypeArray:Array = ["mp3","mp4","flv","wmv","mpeg","mpg","rtf","doc","txt"];
   
   /**
   * Selected file type,Selected from Browsing window
   */
   private var fileType:String;
   
   /**
   * FileReference class object provides browsing and uploading methods
   */
   private var fileRef:FileReference = new FileReference();
   
   /**
   * File Filters Applied for restriction of Files to be browsed
   */

 //FileFilter takes two Arguments 1. Description to be shown in Browse window. 2. List of Extensions
   private var allFileFilter:FileFilter = new FileFilter(“All, *.mp3; *.mp4; *.flv; *.wmv; *.mpeg; *.mpg;”, “*.mp3; *.mp4; *.flv; *.wmv; *.mpeg; *.mpg; “);
   private var videoFilter:FileFilter = new FileFilter(“Video, *.flv; *.wmv; *.mpeg; *.mpg;”, “*.flv; *.wmv; *.mpeg; *.mpg;”);
   private var audioFilter:FileFilter = new FileFilter(“Audio, *.mp3; *.mp4; *.wma;”, “*.mp3; *.mp4; *.wma;”);
   private var textFilter:FileFilter = new FileFilter(“Test, *.rtf; *.doc; *.txt;”, “*.rtf; *.doc; *.txt;”);
   
   /**
   * Function for validating the correct file extension.
   */
   private function validateFileType(fileName:String):Boolean
   {
    txtErr.text = ”;
    txtFileName.text = ”;
    fileName = StringUtil.trim(fileName.toLowerCase());
    var fileExtensionArray:Array = fileName.split(“.”);
    if(fileExtensionArray.length>1)
    {
      fileType= fileExtensionArray[fileExtensionArray.length-1];
     for(var i:int =0;i<fileTypeArray.length;i++)
     {
      if(fileType==fileTypeArray[i])
      {
       txtErr.text = ” File Selected is of proper Extension, Thank You.”;
       txtFileName.text=” Uploaded File Name is =>” + fileRef.name +
       ”\n Size is => “+fileRef.size+” bytes”
       ”\n Creation Date is => “+ fileRef.creationDate +
       ”\n Creator is => “+ fileRef.creator ;
       return true;
      }
     }
    }
    txtErr.text = “You have not Uploaded the file within the standerd extensions provided.”;
    return false;
   }
   
   /**
   * Function Used to open the Browsing window
   */
   private function selectFile():void
   {
    fileRef = new FileReference();
    //browse method excepts the filefilter array as an arguement
                fileRef.browse([allFileFilter,audioFilter,videoFilter,textFilter]);
                fileRef.addEventListener(Event.SELECT, onSelectFile);
   }
   
    
    /**
   * Function used to catch the Select event
   */
   private function onSelectFile(event:Event):void
   {
       validateFileType(fileRef.name);
     }
     
  ]]>
 </mx:Script>
 
 
 <mx:Button x=”63″ y=”86″ label=”Browse”  click=”selectFile()”/>
 <mx:TextArea id=”txtFileName”  editable=”false” x=”161″ y=”114″ width=”360″ height=”97″/>
 <mx:Text color=”#ffffff” selectable=”false” id=”txtErr” x=”161″ y=”63″ width=”397″ height=”25″/>
 <mx:Label x=”143″ y=”47″ selectable=”false” text=”Enter *.* in file name of browsing window to Select wrong file.” fontWeight=”bold” fontSize=”15″/>
 <mx:Label x=”161″ y=”86″ selectable=”false” text=”Retrived Local File Information => “/>
 <mx:Label x=”161″ y=”256″  selectable=”false” text=”Validate wheather the selected file is valid or Not.”/>
</mx:Application>

 

Best Luck )

 Thanks and regards

Shashank Kulkarni

Feel free to mail me :) shashank.pawan@gmail.com

Changing the layout of container dynamically at runtime

Hi,
   Changing the layout of container dynamically at runtime is one of the problem we all regularly meet with.

Suppose we are using the controlls in the HBox where all childrens are lined up horizontally and suddenly at runtime we need to place the children vertically and on some conditions we need to place all the childrens horizontally then it becomes impossible to do it with HBox
   So to come up with such solution it is always easier to do it with container ‘Box’ which is actually the parent container for VBox and HBox.

With Box Container childrens are by default arranged vertically.
With the “direction” property of Box we can change its layout dynamically.

Hmm Sometimes it didnt work then try it by removing the Height and width of of that container

Following is the small Sample to workout this challege

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:application mx=http://www.adobe.com/2006/mxml  pageTitle=”Changing Layout Dynamically” layout=”absolute” creationComplete=”initCombo()”>

 <mx:script> <![CDATA[  
 import mx.utils.StringUtil;  
 import mx.controls.Alert;
 import mx.controls.Image;
 import mx.collections.ArrayCollection; 
 [Bindable]

 private var prefixArray:ArrayCollection = new ArrayCollection([{Item:'Select'},{Item:'Tel'},{Item:'Mob'},{Item:'Fax'},{Item:'Freephone'},{Item:'24 Hour'},{Item:'Sales'},{Item:'Service'},{Item:'Reservations'},{Item:'Emergency'},{Item:'Textphone'}]);

  private function initCombo():void
 {    
     bx.direction=”horizontal”; 
 }      

  private function  changeSize():void 
  {    
            if(StringUtil.trim(txi.text)!=null && txi.length>3)
           {      
                  bx.direction=”vertical”;        
           }       
          else         
         {              
                bx.direction=”horizontal”;
             }
      }    

]]>  </mx:Script> 

<mx:box id=”bx” x=”76″ y=”231″ width=”500″ height=”100″>
       <mx:textinput id=”txi” change=”changeSize()”>   
      <mx:combobox id=”cmbTest” dataprovider=”{prefixArray}” labelfield=”Item”>

</mx:Box>

</mx:Application>

 
Say Cheese :)
Thanks and regards
Shashank Kulkarni.
feel free to mail me shashank.pawan@gmail.com

Solution To Remove Alerts Without commenting it at Release time

April 24, 2008 shashankkulkarni 2 comments

Hi,

We all know When we use Alert.show(“Alert Popped”); If this kind of statement remains uncommented then it creats major problem into the application while deploying it on live server.

Not only this but It also becomes hectic to find and remove all the unwanted Alerts coming out there in the Application specially in large projects. So to avoid such mess I have tried to create the small utility which allows us to avoid such mess.

To Download the application use the following path

http://www.esnips.com/doc/d6cc0890-7c70-495d-b621-23a8e39c8808/DebugUtility

If u likes it, Please reply,

Thanks

Shashank Kulkarni

feel free to mail me :) shashank.pawan@gmail.com

Flex Builder cannot locate the required debug version of the Flash Player problem, Simple Two Step Solution

April 22, 2008 shashankkulkarni 17 comments

Hi, This problem is what regularly me and many Flex guys faces,

C:\WINDOWS\system32\Macromed\Flash\Flash9d.ocx

Flex Builder cannot locate the required debug version of the
Flash Player. You may need to install the debug version of Flash Player 9.0
or reinstall Flex Builder. Do you want to try to debug with the
current version?

Well my personal experience is, After installing the latest flash player version too the issue remains unresolved and i keep getting the same error. lastly I decided to reinstall flex, But amzingly after reinstallling Flex Builder I was facing the same problem and really it was very frustrating,

After spending some time Well its programmers mind afterall may not be great one, I thought before installing new version lets reinstall the First one and thats how i got the solution,

Simple Two steps Solution

1. Reinstall the flash player with flashplayer Uninstaller.

2. Install the Flash plyer.

Wait…….. I know Ur Question would be where to get uninstaller and Latest player version :) then click the following link

http://www.adobe.com/support/flashplayer/downloads.html

Follow the steps mentioned above and :) Here you are ready to Rock Again with debugging facility :)

Best Luck :)

Thanks and regards

Shashank Kulkarni

Feel free to mail me :) shashank.pawan@gmail.com