通过File变量类型ActionScript3从加载的文件中获取大小

时间:2013-03-14 13:50:28

标签: actionscript-3

无法获取我的文件大小!我有一个加载文件的变量,然后在我的fileCompleteLoad事件上,我想检查该文件的大小(.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);    

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);

现在,在那种情况下,我想检查我的文件大小...我尝试过很多东西,但没有成功......有时我从_fileRef.data中得到null。

有什么建议可以解决这个问题吗?

THX

2 个答案:

答案 0 :(得分:2)

为了确保,您是否在data处理程序中获得了onComplete?您显示的代码现在没有这样做。应该是这样的:

_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);
}

答案 1 :(得分:0)

答案是 -

//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

}

现在我可以得到Heigh,witdh等等...... thx!