File Uploader In Flex

This is a great example of a file uploader for Adobe Flex.  It’s in com.everythingflex.components.Uploader.mxml.

<?xml version=”1.0″ encoding=”utf-8″?>
<!– Authored by Rich Tretola (rich@richtretola.com) EverythingFlex.com
Feel free to use within your appplications.  Track changes at EverythingFlex.com

Sample Syntax:
<eFlexComponents:Uploader uploadButtonLabel=”Browse for New Image”
uploadToURL=”http://www.yourdomain.com/uploads/uploader.cfm”
imagesFilter=”*.jpg;*.gif;*.png”
displayNewImage=”true”
displayImagePath=”http://www.yourdomain.coms/uploads”
maxUploadSize=”100000″/>

–>
<mx:Canvas xmlns:mx=”http://www.adobe.com/2006/mxml” width=”100%” height=”100%”>
<mx:Script>
<![CDATA[
import mx.controls.Label;
import mx.controls.Alert;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;

private var uploadURL:URLRequest;
private var file:FileReference;

[Bindable]
public var uploadButtonLabel:String = “Browse for New Image”;
public var uploadToURL:String = “”;
public var imagesFilter:String = “*.jpg;*.gif;*.png”;
public var displayNewImage:Boolean = true;
public var displayImagePath:String = “”;
public var maxUploadSize:Number = 100000000;

public var fileName:String;

public function getFile():void {
if(this.uploadToURL.length > 0){
this.uploadURL = new URLRequest();
this.uploadURL.url = this.uploadToURL;
this.file = new FileReference();
this.configureListeners(file);
this.file.browse(new Array(new FileFilter(”Images”, this.imagesFilter)));
} else {
Alert.show(”uploadToURL is undefined”, “Syntax Error”);
}
}

private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(Event.SELECT, selectHandler);
}
private function selectHandler(event:Event):void {
this.file = FileReference(event.target);
this.fileName = file.name;
this.file.upload(uploadURL);
this.uploadButton.visible = false;
this.progressBar.visible = true;
}
private function completeHandler(event:Event):void {
if(this.displayImagePath.length > 0 && this.displayNewImage == true){
this.newImage.source = this.displayImagePath+’/'+this.fileName;
this.BTN_fileDownload.label = “Try Download”;
this.BTN_fileDownload.visible = true;
this.fileDownloadsuccess.text = “Successfully Uploaded”;
this.fileDownload.text = this.fileName;
this.fileDownloadurl.text = this.displayImagePath+’/'+this.fileName;
/*var u:URLRequest = new URLRequest(”{this.displayImagePath+’/'+this.fileName}”);
this.BTN_fileDownload.change =”navigateToURL(u,’_self’)” */
}
this.progressBar.visible = false;
this.uploadButton.visible = true;
}
private function progressHandler(event:ProgressEvent):void {
if(event.bytesTotal > this.maxUploadSize){
this.cancelUpload();
Alert.show(”Max size allowed: ” + this.maxUploadSize + ” bytes. \nYour file is ” + event.bytesTotal + ” bytes”, “Upload cancelled, File too large”);
} else {
this.progressBar.setProgress(event.bytesLoaded,event.bytesTotal);
this.progressBar.label = event.bytesLoaded + ” of ” + event.bytesTotal + ” bytes loaded”;
}
}
private function ioErrorHandler(event:IOErrorEvent):void {
Alert.show(”ioErrorHandler: ” + event, “IO Error”);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
Alert.show(”securityErrorHandler: ” + event, “Security Error”);
}
private function cancelUpload():void{
this.file.cancel();
this.progressBar.visible = false;
this.uploadButton.visible = true;
}
private function linkTo():void{
var linkurl:URLRequest = new URLRequest(this.fileDownloadurl.text);
navigateToURL(linkurl,”_self”);
}
]]>
</mx:Script>
<mx:Button id=”uploadButton” click=”this.getFile()” label=”{this.uploadButtonLabel}” visible=”true” />

<mx:ProgressBar width=”200″ id=”progressBar” mode=”manual” visible=”false”/>

<mx:VBox x=”10″ y=”30″>
<mx:Image id=”newImage” visible=”false”/>
<mx:Text id=”fileDownloadsuccess”/>
<mx:Text id=”fileDownload”/>
<mx:Text id=”fileDownloadurl”/>
<mx:Button id=”BTN_fileDownload” visible=”false” label=”Download” click=”linkTo()”/>
</mx:VBox>

</mx:Canvas>

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • Live
  • MySpace
  • Turn this article into a PDF!
  • Reddit
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz

18 Responses to “File Uploader In Flex”

  1. congratulated@stimulants.moderns” rel=”nofollow”>.…

    thanks for information!!…

  2. tatler@extraneousness.campaigned” rel=”nofollow”>.…

    tnx for info!…

  3. rockfork@ahm.korngold” rel=”nofollow”>.…

    ñýíêñ çà èíôó!!…

  4. vibrato@agoeng.gypsum” rel=”nofollow”>.…

    ñïàñèáî çà èíôó!…

  5. anton@review.sforzando” rel=”nofollow”>.…

    hello!!…

  6. excitement@lev.ungracious” rel=”nofollow”>.…

    ñïàñèáî….

  7. empathy@gloucester.sickening” rel=”nofollow”>.…

    áëàãîäàðåí!…

  8. seas@automotive.tends” rel=”nofollow”>.…

    tnx!!…

  9. sonambula@shutdown.yancy” rel=”nofollow”>.…

    áëàãîäàðþ!!…

  10. batch@depart.burman” rel=”nofollow”>.…

    hello!…

  11. freely@mounting.disking” rel=”nofollow”>.…

    thanks….

  12. limping@telemann.engages” rel=”nofollow”>.…

    thank you….

  13. chaffing@alveolus.sides” rel=”nofollow”>.…

    ñýíêñ çà èíôó!…

  14. onslaughts@ossify.diffusely” rel=”nofollow”>.…

    ñýíêñ çà èíôó….

  15. enormity@disabled.sulfide” rel=”nofollow”>.…

    ñïñ çà èíôó!…

  16. pleads@squares.komurasaki” rel=”nofollow”>.…

    thank you….

  17. aida@peculiarity.max” rel=”nofollow”>.…

    tnx!…

  18. convalescence@faneuil.strutted” rel=”nofollow”>.…

    thank you!!…

Leave a Reply

You must be logged in to post a comment.