2011-03-28 32 views
0

我試圖在Flex 4中上傳圖片,但尚未獲得如此多的成功。有人可以幫忙嗎?如何使用Filereference上傳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!");      
       } 

      } 

這很適合當我把web服務的方法,並存儲在SQLServer中的圖像(=字節陣列)。

+0

你能澄清一下這個問題嗎?你希望你的Flex客戶端上傳一個文件到服務器,還是你想將客戶端產生的圖像保存到本地磁盤? – Dan 2011-03-28 21:30:13

+0

我想將上傳的圖像保存在服務器上的磁盤上。 – marko 2011-03-29 05:19:01

回答

1

捕獲圖像並上傳它,你可能想看看ImageSnapshot。 它會將圖像編碼爲JPEG或PNG。保存該字節數組將爲您提供更大的靈活性,並將存儲在服務器中的數據從客戶端的實現中分離出來。