Total Pageviews

Wednesday 13 July 2011

Uploading image file in Flex

The file upload is initiated when the user clicks on the Upload button on the Stage, which invokes the FileUpload.startUpload() method. This method calls the browse() method of the FileReference class which causes the operating system to display a system dialog box prompting the user to select a file to upload to the remote server. The following excerpt shows the code for the startUpload() method:

public function startUpload():void
{
   fr.browse();  
}

Once the user selects a file to upload, the select event (Event.SELECT) is dispatched, causing the selectHandler() method to be invoked. The selectHandler() method creates a new URLRequest object and sets the URLRequest.url property to the value of the UPLOAD_URL constant defined earlier in the code. Finally, the FileReference object uploads the selected file to the specified server-side script. The code for the selectHandler() method is as follows:

private function selectHandler(event:Event):void
{
    var request:URLRequest = new URLRequest();  
    request.url = UPLOAD_URL;  
    fr.upload(request);  
}

The remaining code in the FileUpload class is the same as the code defined in the FileDownload class. If a user wishes to terminate the upload at any point, they can click the Cancel button, which sets the label on the progress bar and stops the file transfer immediately. The progress bar gets updated whenever the progress event (ProgressEvent.PROGRESS) is dispatched. Similarly, once the upload has completed, the progress bar is updated to notify the user that the file has uploaded successfully. The Cancel button is then disabled until the user begins a new file transfer.

Saturday 2 July 2011

Convert an Image to Base64 string and vice versa

import flash.utils.ByteArray;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event; 
import mx.utils.Base64Decoder; //Transform String in a ByteArray.
import mx.utils.Base64Encoder; //Transform ByteArray in a readable string.
import mx.graphics.codec.PNGEncoder; //Encode a BitmapData in a PNG;

//In this case I had a BitmapData in the library
so I took it from there.
var myBMD:BitmapData=new myBitmapData(0,0); 

//Create the ByteArray of myBMD usinf the PNGEncoder.
var myBMDByteArray:ByteArray=(new PNGEncoder).encode(myBMD);

var compStr:String=compress(myBMDByteArray); //Creates String
trace(compStr);

// The loader acts exactly as we were loading an external 
PNG except the fact that we write the ByteArray.
var loader:Loader = new Loader(); 
// In the loadBytes we write the Base64 String of the image.
loader.loadBytes(uncompress(compStr));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(e:Event):void
{
    var bmp:BitmapData=new BitmapData(loader.width,loader.height,true,0x0);
    bmp.draw(loader);
    addChild(new Bitmap(bmp));
}

// Compress a ByteArray into a Base64 String.
function compress(bytes:ByteArray):String
{
    var enc:Base64Encoder = new Base64Encoder();   
    enc.encodeBytes(bytes);
    return enc.drain().split("\n").join("");
}

// Uncompress a Base64 String into a ByteArray.
function uncompress(str:String):ByteArray
{
    var dec:Base64Decoder = new Base64Decoder();
    dec.decode(str);
    var newByteArr:ByteArray=dec.toByteArray();       
    return newByteArr;
}


Compatibility issue with ios applications developed using flash builder 4.5

Building an app using Flashbuilder 4.5 and just tried to install it on iOS devices it says its not compatible with my ipod touch while iOS devices are running 4.2 and all are the 3rd generation, devices.

Reasonn is Flash Builder 4.5 applications will work only newer version of iOS devices with multitasking hardware capabilities. i.e. your devices (iPhone 4 ,iPod touch and iPad) should be with newer version hardware capabilities.

Microsoft Logo using flexbox and Reactjs

 <!DOCTYPE html> <html> <head>     <script src="https://unpkg.com/react@18/umd/react.development.js" crossori...