FileReference中的警告 - 找不到原因

时间:2010-10-03 06:58:05

标签: flex file-upload flash-builder

我正在尝试制作几张照片的加载器(使用FileReference)。我收到了警告,但我不知道他们出现的原因。

警告:无法绑定到类'Object'上的属性'fr'(类不是IEventDispatcher) 警告:无法绑定到类'flash.net::FileReference'上的属性'name' 警告:无法绑定到类'flash.net::FileReference'上的属性'data' 警告:无法绑定到类'Object'上的属性'fr'(类不是IEventDispatcher) 警告:无法绑定到类'flash.net::FileReference'上的属性'name' 警告:无法绑定到类'flash.net::FileReference'上的属性'data'

import mx.events.CollectionEvent;
import flash.net.FileReferenceList;
import mx.collections.ArrayCollection;

[Bindable]
private var photos:ArrayCollection = new ArrayCollection;
private var frList:FileReferenceList = new FileReferenceList;

private function init():void
{
photos.addEventListener(CollectionEvent.COLLECTION_CHANGE,function():void
{ 
startUploadButton.enabled = (photos.length>0);
clearPhotosButton.enabled = (photos.length>0);
});
frList.addEventListener(Event.SELECT,addPhotos);
}

private function selectPhotos():void
{
var fileFilter:FileFilter = new FileFilter("Images jpeg","*.jpg;*.jpeg");
frList.browse([fileFilter]);
}

private function addPhotos(e:Event):void
{
for (var i:uint = 0; i < frList.fileList.length; i++)
{
var elem:Object = new Object;
elem.fr = FileReference(frList.fileList[i]);
elem.fr.load();
elem.fr.addEventListener(Event.COMPLETE,refreshThumb);
photos.addItem(elem);
}
}

private function refreshThumb(e:Event):void
{
photosList.invalidateList();
}

public function clearPhoto(data:Object):void
{
photos.removeItemAt(photos.getItemIndex(data));
photosList.invalidateList();
}

private function startUpload():void
{
photosProgressContainer.visible = true;
var request:URLRequest = new URLRequest();
request.url = "http://localhost/tempLoader-debug/upload.php";
var fr:FileReference = photos.getItemAt(0).fr;
fr.cancel();
fr.addEventListener(ProgressEvent.PROGRESS,uploadProgress);
fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadComplete);
fr.upload(request);
}

private function uploadProgress(e:ProgressEvent):void
{
photosProgress.setProgress(e.bytesLoaded,e.bytesTotal);
}

private function uploadComplete(e:DataEvent):void
{
photos.removeItemAt(0);
photosList.invalidateList();
if (photos.length > 0) 
startUpload();
else
photosProgressContainer.visible = false;
}

1 个答案:

答案 0 :(得分:1)

出现此警告是因为您尝试绑定到项呈示器或某处的属性frFileReference.nameFileReference.data。它可能不会打扰你(不知道你所有的代码),但要避免它们执行以下操作:

强类型数据提供程序

使用特殊类的对象填充photos,如:

public class Photo
{
    public function Photo(fileReference:FileReference)
    {
        this.fileReference = fileReference;
    }

    public var fileReference:FileReference;

    [Bindable("__NoChangeEvent__")] // __NoChangeEvent__ is a special name
    public function get name():String
    {
        return fileReference.name;
    }

    [Bindable("__NoChangeEvent__")]
    public function get data():*
    {
        return fileReference.data;
    }
}

然后替换代码:

var elem:Object = new Object;
elem.fr = FileReference(frList.fileList[i]);
elem.fr.load();
elem.fr.addEventListener(Event.COMPLETE,refreshThumb);
photos.addItem(elem);

以下内容:

var elem:Photo = new Photo(frList.fileList[i]);
elem.fileReference.addEventListener(Event.COMPLETE,refreshThumb);
elem.fileReference.load();
photos.addItem(elem);

您还应该相应地更改使用photos集合的所有代码。