Hello All,

          I have been trying to work out  where, a user uploads an
image, and then when upload is clicked, the image is resized to 2
different thumbnails and saved to s3.

          Unfortunately I am stuck in resizing and uploading. This i
am trying in Flash 10, with its new File Ref added feature.


I request if you could guide me through. Pasting the code for ref:



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
backgroundAlpha="0" creationComplete="registerCallbacks();"
layout="absolute">

<mx:Script>
        <![CDATA[
            //call an ActionScript function in Flash Player, using JavaScript
in the HTML page
                import flash.external.ExternalInterface;
                //provides a means to upload and download files between a user's
computer and a server
                //import flash.net.FileReference;
                import com.adobe.net.MimeTypeMap;
                import com.elctech.S3UploadOptions;
                import com.elctech.S3UploadRequest;

                //The following is part 2
        import mx.controls.Alert;
        //formats a valid number by adjusting the decimal rounding and
precision
        import mx.formatters.NumberFormatter;
        //encoders raw bitmaps as encoded images using Portable
Network Graphics (PNG) lossless compression
        import mx.graphics.codec.PNGEncoder;

        // encoder to create file to save
        private var png:PNGEncoder = new PNGEncoder();

        // allowed file types
        private var fileTypes:FileFilter = new FileFilter("Image",
"*.jpg;*.jpeg;*;*.gif;*.png;*");

        // FileReference classes for upload and download
        [Bindable]
        private var loadFileRef:FileReference = new FileReference();
                private var saveFileRef:FileReference = new FileReference();

        private function dupeImage(source:Image):void {
            var data:BitmapData = Bitmap(source.content).bitmapData;
            var bitmap:Bitmap = new Bitmap(data);
        }

                private var mimeMap:MimeTypeMap = new MimeTypeMap();
                private var options:S3UploadOptions = new S3UploadOptions();
                //private var fileReference:FileReference;
                private function registerCallbacks():void {
                    if (ExternalInterface.available) {
                      ExternalInterface.addCallback("init", init);
                      ExternalInterface.addCallback("upload", upload);
                      ExternalInterface.call('s3_swf.init');
                    }
                }

                private function init(url:String, initialMessage:String="mxml",
doChecks:String="0",
                    onSuccess:String="", onFailed:String="",
                    onSelected:String="", onCancel:String=""
                        ):void {
                    options.SignatureQueryURL = url;
                        userMessage.text = initialMessage;
                        options.doChecks = doChecks;
                    if (onSuccess  != "") { options.onSuccessCall  = onSuccess; 
 }
                    if (onFailed   != "") { options.onFailedCall   = onFailed;  
 }
                    if (onSelected != "") { options.onSelectedCall = 
onSelected; }
                    if (onCancel   != "") { options.onCancelCall   = onCancel;  
 }

                }

                private function browser():void {
                    //this.fileReference = new FileReference();
                    userMessage.setStyle("color","#222222");

                    // setup file reference event handlers
            image.source=null;


                    loadFileRef.addEventListener(Event.SELECT, function
(event:Event):void {
                        // set options.FileName
                        options.FileName = 
loadFileRef.name.replace(/^.*(\\|\/)/gi,
'').replace(/[^A-Za-z0-9\.\-]/gi, '_');
                        userMessage.text   = options.FileName;

                        // set options:FileSize
                        options.FileSize = loadFileRef.size.toString();

                        // set options.ContentType
                        var FileNameArray:Array = options.FileName.split(/\./);
                        var FileExtension:String = FileNameArray
[FileNameArray.length - 1];
                        options.ContentType = 
mimeMap.getMimeType(FileExtension);

                        loadFileRef.load();

                        trace(options.onSelectedCall);
                        ExternalInterface.call(options.onSelectedCall,
options.FileName, options.FileSize, options.ContentType);
                    });

                    loadFileRef.addEventListener(Event.COMPLETE, function
(event:Event):void {
                        // set options.FileName
                        image.source = loadFileRef.data;

                    });

                    loadFileRef.browse([fileTypes]);
                    //fileReference.browse();
                }

        private function capture( maxWidth:Number,
maxHeight:Number):void{
                        var scaleFactor:Number = 1;
                        var newWidth:Number = maxWidth;
                        var newHeight:Number = maxHeight;
                        if(image.width > image.height) {
                                scaleFactor = maxWidth / image.width;
                        }
                        else {
                                scaleFactor = maxHeight / image.height;
                        }
                        newWidth = image.width * scaleFactor;
                        newHeight = image.height * scaleFactor;
                        var scaledBitmapData:BitmapData = new 
BitmapData(newWidth,
newHeight);
                        var scaleMatrix:Matrix = new Matrix();
                        scaleMatrix.scale(scaleFactor, scaleFactor);
                        scaledBitmapData.draw(image, scaleMatrix);
                        //image.bitmapData = scaledBitmapData;
                        var ba:ByteArray = png.encode(scaledBitmapData);
                        // split off the original extension and replace with png
                        var ext:String = loadFileRef.name.split(".").pop();
                        loadFileRef.name.replace(ext,"png");

                        
//saveFileRef.save(ba,loadFileRef.name.replace(ext,"png"));


                }

                private function upload(prefixPath:String = ""):void {

                    // return if user has not selected any file
                    if (options.FileSize == null) {
                               userMessage.text = 'You need to select
a file!';
                        trace(options.onFailedCall);
                        ExternalInterface.call(options.onFailedCall);
                        return;
                    }

                       userMessage.text = 'Initiating...';

                            // ======================== //
                            // Start Query S3 Signature //
                            // ======================== //
                            var request:URLRequest     = new URLRequest
(options.SignatureQueryURL);
                            var loader:URLLoader       = new URLLoader();
                            var variables:URLVariables = new URLVariables();

                            options.PrefixPath         = prefixPath; // reset
options.PrefixPath
                            options.key                = options.PrefixPath +
options.FileName;

                            variables.file_name        = options.FileName;
                            variables.file_size        = options.FileSize;
                            variables.key              = options.key;
                            variables.content_type     = options.ContentType;
                            variables.do_checks        =
options.doChecks;

                            request.method             = URLRequestMethod.GET;
                            request.data               = variables;
                            loader.dataFormat          = 
URLLoaderDataFormat.TEXT;

                            configureListeners(loader);
                            loader.load(request);
                        }

                        private function configureListeners
(dispatcher:IEventDispatcher):void {
                            dispatcher.addEventListener(Event.COMPLETE, 
completeHandler);
                            dispatcher.addEventListener(Event.OPEN, 
openHandler);
                            dispatcher.addEventListener(ProgressEvent.PROGRESS,
progressHandler);
                            
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
securityErrorHandler);
                            
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS,
httpStatusHandler);
                            dispatcher.addEventListener(IOErrorEvent.IO_ERROR,
ioErrorHandler);
                        }

                        private function completeHandler(event:Event):void {
                            var loader:URLLoader = URLLoader(event.target);
                            var xml:XML  = new XML(loader.data);

                            options.policy         = xml.policy;
                            options.signature      = xml.signature;
                            options.bucket         = xml.bucket;
                            options.AWSAccessKeyId = xml.accesskeyid;
                            options.acl            = xml.acl;
                            options.Expires        = xml.expirationdate;
                            options.Secure         = xml.https;

                                if (xml.errorMessage!="")
                                {
                                        uploadProgressBar.visible = false;
                                        selectButton.visible = true;
                                        userMessage.visible = true;
                                        userMessage.text = "Error: " + 
xml.errorMessage;
                                        userMessage.setStyle("color","#770000");
                                        
ExternalInterface.call(options.onFailedCall);
                                        return;
                                }
                            // ===================== //
                            // Start post file to S3 //
                            // ===================== //
                            var request:S3UploadRequest = new 
S3UploadRequest(options);

                            request.addEventListener(Event.OPEN, 
function(event:Event):void
{
                                userMessage.text = "";
                                uploadProgressBar.visible = true;
                                selectButton.visible = false;
                                trace(event);
                            });
                            request.addEventListener(ProgressEvent.PROGRESS, 
function
(event:ProgressEvent):void {
                                var penct:uint = uint(event.bytesLoaded / 
event.bytesTotal *
100);
                                uploadProgressBar.label = 'Uploading ' + penct 
+ " %";
                                uploadProgressBar.setProgress(event.bytesLoaded,
event.bytesTotal);
                            });
                            request.addEventListener(IOErrorEvent.IO_ERROR, 
function
(event:IOErrorEvent):void {
                                uploadProgressBar.visible = false;
                                selectButton.visible = true;
                                userMessage.text = "Upload Error, please 
retry.";
                                trace(options.onFailedCall);
                                ExternalInterface.call(options.onFailedCall);
                                trace(event);
                            });
                            
request.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
function(event:SecurityErrorEvent):void {
                                uploadProgressBar.visible = false;
                                selectButton.visible = true;
                                userMessage.text = "Upload error, Access 
denied.";
                                trace(options.onFailedCall);
                                ExternalInterface.call(options.onFailedCall);
                                trace(event);
                            });
                            
request.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,
function(event:Event):void {
                                uploadProgressBar.visible = false;
                                //selectButton.visible = true;
                                userMessage.text = "Upload complete!";
                                trace(options.onSuccessCall);
                                ExternalInterface.call(options.onSuccessCall,
options.FileName, options.FileSize, options.ContentType);
                                trace(event);
                            });

                            try {


                                          userMessage.text = "Uploading 
commenced...";


                              var scaleFactor:Number = 1;
                                                          var newWidth:Number = 
100;
                                                          var newHeight:Number 
= 100;
                                                          if(image.width > 
image.height) {
                                                                scaleFactor = 
100 / image.width;
                                                           }
                                                          else {
                                                                scaleFactor = 
100 / image.height;
                                                        }
                                                        newWidth = image.width 
* scaleFactor;
                                                        newHeight = 
image.height * scaleFactor;
                                                        var 
scaledBitmapData:BitmapData = new BitmapData(newWidth,
newHeight);
                                                        var scaleMatrix:Matrix 
= new Matrix();
                                                        
scaleMatrix.scale(scaleFactor, scaleFactor);
                                                        
scaledBitmapData.draw(image, scaleMatrix);
                                                        //image.bitmapData = 
scaledBitmapData;
                                                        var ba:ByteArray = 
png.encode(scaledBitmapData);
                                                        // split off the 
original extension and replace with png
                                                        var ext:String = 
loadFileRef.name.split(".").pop();
                                                        
loadFileRef.name.replace(ext,"png");
                                        //loadFileRef.upload(request, "file", 
false);
                             request.upload(ba);


                                        scaleFactor = 1;
                                                        newWidth = 200;
                                                        newHeight = 200;
                                                        if(image.width > 
image.height) {
                                                                scaleFactor = 
200 / image.width;
                                                        }
                                                        else {
                                                                scaleFactor = 
200 / image.height;
                                                                }
                                                        newWidth = image.width 
* scaleFactor;
                                                        newHeight = 
image.height * scaleFactor;
                                                        var 
scaledBitmapDat:BitmapData = new BitmapData(newWidth,
newHeight);
                                                        var scaleMatric:Matrix 
= new Matrix();
                                                        
scaleMatric.scale(scaleFactor, scaleFactor);
                                                        
scaledBitmapDat.draw(image, scaleMatric);
                                                        //image.bitmapData = 
scaledBitmapDat;
                                                        var bas:ByteArray = 
png.encode(scaledBitmapDat);
                                                        // split off the 
original extension and replace with png
                                                        var exts:String = 
loadFileRef.name.split(".").pop();
                                                        var mns:String = 
loadFileRef.name.split(".")[0];
                                                        
loadFileRef.name.replace(mns, mns + "thumb1");
                                                        
loadFileRef.name.replace(exts,"png");


                                                                
request.upload(loadFileRef);




                            } catch(e:Error) {
                                uploadProgressBar.visible = false;
                                selectButton.visible = true;
                                userMessage.text = "" + e;
                                userMessage.setStyle("color","#770000");
                                trace("An error occurred: " + e);
                            }
                        }

                        private function openHandler(event:Event):void {
                           userMessage.text = "Preparing for
upload...";
                            trace("openHandler: " + event);
                        }
                        private function 
progressHandler(event:ProgressEvent):void {
                            trace("progressHandler loaded:" + event.bytesLoaded 
+ " total:
" + event.bytesTotal);
                        }
                        private function securityErrorHandler
(event:SecurityErrorEvent):void {
                            trace("securityErrorHandler: " + event);
                            trace(options.onFailedCall);
                            userMessage.text = "Whoa - something went
really wrong preparing for upload: securityErrorHandler: " + event;
                            ExternalInterface.call(options.onFailedCall);
                        }
                        private function 
httpStatusHandler(event:HTTPStatusEvent):void {
                            trace("httpStatusHandler: " + event);
                        }
                        private function 
ioErrorHandler(event:IOErrorEvent):void {
                            trace("ioErrorHandler: " + event);
                            trace(options.onFailedCall);
                            userMessage.text = "Whoa - something went
really wrong preparing for upload: ioErrorHandler: " + event;
                            ExternalInterface.call(options.onFailedCall);
                        }

        ]]>


  </mx:Script>


            <mx:Image id="image" visible="false"/>
            <mx:Label x="100" y="10" id="userMessage" color="#222222"
fontSize="12" text=""/>
            <mx:Button x="10" y="7" label="Browse..." click="browser()"
id="selectButton"/>
            <mx:ProgressBar x="1" y="7" width="486" height="20"
labelPlacement="center" id="uploadProgressBar" visible="false"
mode="manual" label="Uploading..."/>

</mx:Application>





Thanks,
'V









--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to