【问题标题】:How to display an image in Flash datagrid after selecting an image for uploading?选择要上传的图像后如何在 Flash 数据网格中显示图像?
【发布时间】:2011-01-21 16:55:57
【问题描述】:

浏览图片并选择它们后。如何在数据网格中显示图像的预览?我能够显示文件名、文件大小,但无法显示图像。以下是我编写的代码,请注意它不是完整的代码,但足以使其易于理解。谢谢。

// variables used
var list:Array = new Array();
var listDP:Array = new Array();
var fileRefList:FileReferenceList;

//dgMain is my DataGrid instance
dgMain.addColumn("name");
dgMain.addColumn("size");

list = fileRefList.fileList; // List of files that user has selected

for(var i:Number = 0; i < list .length; i++) 
{
   list_dp.push({name:list[i].name, size:Math.round(list[i].size / 1000) + " kb"});
}

dgMain.dataProvider = new DataProvider(list_dp);
dgMain.spaceColumnsEqually();

【问题讨论】:

    标签: flash actionscript-3 image datagrid upload


    【解决方案1】:

    好问题。当您在FileReferenceList 上听到Event.SELECT 事件时,只需加载每个图像。然后为datagrid创建一个itemRenderer,可以显示返回的ByteArray。这是一个例子:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application
        xmlns:mx="http://www.adobe.com/2006/mxml"
        creationComplete="creationCompleteHandler()">
    
    
        <mx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
    
                // variables used
                var list:Array;
                var files:ArrayCollection = new ArrayCollection();
                var fileReferenceList:FileReferenceList = new FileReferenceList();
    
                protected function creationCompleteHandler():void
                {
                    fileReferenceList.addEventListener(Event.SELECT, selectHandler);
                }
    
                protected function selectHandler(event:Event):void
                {
                    load();
                }
    
                protected function completeHandler(event:Event):void
                {
                    var fileReference:FileReference = event.target as FileReference;
                    var token:Object = {name:fileReference.name, size:Math.round(fileReference.size / 1000) + " kb", preview:fileReference.data};
                    files.addItem(token);
                }
    
                public function load():void
                {
                    list = fileReferenceList.fileList; // List of files that user has selected
                    var i:int = 0;
                    var n:int = list.length
                    for(i; i < n; i++) 
                    {
                        list[i].addEventListener(Event.COMPLETE, completeHandler);
                        list[i].load();
                    }
                }
    
            ]]>
        </mx:Script>
    
        <mx:Button label="browse" click="fileReferenceList.browse()"/>
    
        <mx:DataGrid id="dgMain"
            dataProvider="{files}"
            horizontalScrollPolicy="on"
            allowMultipleSelection="true" rowHeight="25">
            <mx:columns>
                <mx:DataGridColumn dataField="preview" headerText="preview">
                    <mx:itemRenderer>
                        <mx:Component>
                            <mx:Canvas verticalCenter="0" horizontalScrollPolicy="off" verticalScrollPolicy="off">
                                <mx:Image source="{data.preview}" width="20" height="20"/>
                            </mx:Canvas>
                        </mx:Component>
                    </mx:itemRenderer>
                </mx:DataGridColumn>
                <mx:DataGridColumn dataField="name" headerText="Name" />
                <mx:DataGridColumn dataField="size" headerText="Size" />
            </mx:columns>
        </mx:DataGrid>
    </mx:Application>
    

    如果这解决了问题,请将其标记为正确:)。

    最好, 兰斯

    【讨论】:

    • 嗨兰斯,感谢您的回复。由于我对 Flash 很陌生并且没有 Flex 的软件,所以上面的代码是 Flex 的吗?我也可以对 Flash 做同样的事情吗?如果是这样,我该如何在我的 Flash Datagrid 中使用等效的 itemRenderer。目前,我已经有一个 DataGrid 组件。谢谢!
    • 嘿,我从未使用过 Flash,所以我不确定 Flash 中数据网格的细节。但是 completeHandler 和 load 方法中 FileReferenceList 的代码应该没问题。如果您尝试过,请告诉我。
    • 嗨 Lance,我设法获得了 Flex 并尝试了您的代码。但是我在 var token:Object = {name:fileRef.name, size:Math.round(fileRef.size / 1000) + " kb", preview:fileRef.data};我遇到的错误是“通过静态类型 flash.net:FileReference 的引用访问可能未定义的属性数据”。我目前使用的是 Flash 版本 10.0.2.54。
    • 我已经通过更新我的 Flex SDK 解决了这个问题。谢谢。由于我没有足够的声誉,我无法将您的答案标记为正确,我对 stackoverflow 还是很陌生。
    • 很高兴听到!您需要多少声望才能将答案标记为正确?
    猜你喜欢
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 2018-07-05
    • 2020-06-24
    • 2020-11-13
    • 2023-01-04
    • 2012-07-07
    • 1970-01-01
    相关资源
    最近更新 更多