0

我正在尝试在 Flex 4 中上传图像,但还没有取得那么大的成功。有谁能帮忙?

我的代码:

  private var fileref:FileReference; 

                protected function application1_creationCompleteHandler(event:FlexEvent):void
                {
                    fileref = new FileReference(); 
                }


                protected function button1_clickHandler(event:MouseEvent):void
                {
                    fileref.browse(); 
                    fileref.addEventListener(Event.SELECT, fileSelect); 
                }

                private function fileSelect(e:Event):void {
                    try {
                    var data:ByteArray = e.target as ByteArray; 
                    fileref.save(data, "pic1.jpg");             
                    }
                    catch(err:Error) {
                        Alert.show("Error: " + err.message); 
                    }
                }

编辑:

这真的很简单:

private function fileComplete(e:Event):void {
                if(fileref.data != null) {
                image1.data = fileref.data; 
                }


            }

我制作了第二个按钮来保存图像,它工作正常,但我打开了对话框,真的有必要吗?如何防止它并将其明确放在服务器光盘上? 另一种方法(我使用 .NET 作为后端)获取 bytearray-image 并通过 .net webservice 发送它,然后让 C# 代码保存图像。也许这是一个更好的选择。Actionscript 3 可能对它的功能有一些限制,或者我真的不知情?

protected function button2_clickHandler(event:MouseEvent):void
            {
                fileref = new FileReference(); 
                var data:ByteArray = image1.data as ByteArray; 
                if(data != null) {
                    fileref.save(data); 
                }
                else {
                    Alert.show("Picture is null!");                     
                }

            }

当我采用 Webservice 方法并将图像(= bytearray)存储在 SqlServer 中时,这很有效。

4

1 回答 1

1

要捕获图像并将其上传,您可能需要查看ImageSnapshot. 它将图像编码为JPEG或PNG。保存该字节数组将为您提供更大的灵活性,并将存储在服务器中的数据与客户端的实现分离。

于 2011-03-29T08:36:27.050 回答