Marko
Marko

Reputation: 10992

How to upload images in Flex 4 with Filereference?

I'm trying to upload images in Flex 4 and haven't got so much success with it yet. Somebody who can help?

My code:

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

Edit:

This was real simple todo:

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


            }

I made a second button which should save the image, and it works fine, but I get the dialog up, is it really necessary? How can I prevent it and put it explicitly on the servers disc? An alternative (I use .NET as a backend) to take the bytearray-image and send it through a .net webservice and let the C# code save the image. Maybe that's an better option. Actionscript 3 may have some limitations on what it can do, or am I not really informed?

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!");                     
                }

            }

This worked well when I took the Webservice approach and stored the Image (= bytearray) in SqlServer.

Upvotes: 0

Views: 1876

Answers (1)

Dan
Dan

Reputation: 1161

to capture an image and upload it you may want to have a look at ImageSnapshot. It will encode the image into either JPEG or PNG. Saving that bytearray will give you more flexibility and decouple the data stored in the server from the client's implementation.

Upvotes: 1

Related Questions