RafaelFelisbino
RafaelFelisbino

Reputation: 29

Getting a size from a loaded file via a File variable type ActionScript3

Cannot get my file size! I have a variable that loads the file, and then on my fileCompleteLoad event i want to check the size of that file (.png).

// clickButton event to load the file

public function onMouseClick(e:MouseEvent):void{
    _fileRef = new File();
    _fileRef.addEventListener(Event.SELECT, onFileSelected, false, 0, true);
    _fileRef.addEventListener(Event.CANCEL, onCancel, false, 0, true);
    _fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
    _fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, 
    onSecurityError,  false, 0, true);

    _fileRef.browse([_imageFilter]);
}

// selected event

public function onFileSelected(evt:Event):void
{
    _fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);
    _fileRef.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
    _fileRef.load();
}


// thats my eventComplete

 public function onComplete(evt:Event):void
{
    _msgSuccessErrorTextField.text = "File was successfully loaded.";
    _pngInputTextField.text = String(_fileRef.nativePath);
    _atfOutputTextField.text = _fileRef.nativePath.replace(".png",".atf");
    _inputNativeProcess = _fileRef.nativePath;
    _outputNativeProcess = _atfOutputTextField.text;
    _flagLoadedFile = new Boolean(true);

    var test:Bitmap = evt.target.data as Bitmap;
    if(test){
        trace(test.height);
    }

    _fileRef.removeEventListener(Event.SELECT, onFileSelected);
    _fileRef.removeEventListener(ProgressEvent.PROGRESS, onProgress);
    _fileRef.removeEventListener(Event.COMPLETE, onComplete);
    _fileRef.removeEventListener(Event.CANCEL, onCancel);    

Now, in that event i want to check my file size... I ve tried many things but didnt get success... and sometimes i get null from my _fileRef.data.

Any suggestions to solve that?

thx

Upvotes: 0

Views: 444

Answers (2)

RafaelFelisbino
RafaelFelisbino

Reputation: 29

the Answer is -

//add that on my public function onComplete(evt:Event):void{}

var loader:Loader = new Loader();

loader.loadBytes(byteArray);

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

// build another event

publicfunction loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);

var bitmapData:BitmapData = new BitmapData(loaderInfo.width,
loaderInfo.height, false,   0xFFFFFF);

bitmapData.draw(loaderInfo.loader);
// result: bitmapData

}

now i can get the Heigh, witdh and whatever... thx!

Upvotes: 0

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

Just to make sure, are you getting the data inside the onComplete handler? The code you show doesn't do that right now. Should be something like :

_fileRef.addEventListener(Event.COMPLETE, onComplete, false, 0, true);

private function onComplete(e:Event):void
{
    var test:Bitmap = e.target.data as Bitmap;
    if(test)
        trace(test.height);
}

Upvotes: 2

Related Questions