2014-03-19 32 views
0

我有一個FileReference選擇圖像在我的形式。如果我沒有選擇任何圖像文件提交表單,它將以編程方式加載另一個圖像。Flex 4 - FileReference數據返回空值

我的代碼:

 <s:Form id="mainForm" height="100%" width="100%" left="10%" right="10%" top="10%"> 
     <s:FormItem id="nameLabel" label="Employee Name"> 
      <s:TextInput id="employeeName" rollOver="validateAndSubmit()"/> 
     </s:FormItem> 
     <s:FormItem id="imageLabel" label="Image"> 
      <mx:HBox> 
       <s:TextInput id="employeeImageName" editable="false" showErrorSkin="true" showErrorTip="false"/> 
       <s:Button id="imageButton" label="Browse" click="onBrowseButtonClicked(event)"/> 
      </mx:HBox > 
     </s:FormItem> 
     <s:FormItem> 
      <s:layout> 
       <s:HorizontalLayout gap="10"/> 
      </s:layout> 
      <s:Button id="submitButton" label="Submit" click="storeInputs(event)"/> 
      <s:Button id="clearButton" label="clear" click="clearInputs()"/> 
     </s:FormItem> 
    </s:Form> 
    <s:DataGrid width="100%" height="100%" dataProvider="{arrayCollection}"> 
    <s:columns> 
     <s:ArrayList> 
      <s:GridColumn headerText="Name" dataField="name" /> 
      <s:GridColumn headerText="Employee" id="imageColumn" dataField="imageData"/> 
      <s:itemRenderer> 
       <fx:Component> 
        <s:GridItemRenderer>          
         <s:Image id="image" source="{data.imageData}" visible="true" height="80" width="100"/> 
        </s:GridItemRenderer> 
       </fx:Component> 
      </s:itemRenderer> 
     </s:ArrayList> 
    </s:columns> 
</s:DataGrid> 

ActionScript代碼像

public var loadFile:FileReference; 
    [Bindable]private var arrayCollection : ArrayCollection = new ArrayCollection(); 

    private function onBrowseButtonClicked(event : MouseEvent) : void 
    { 
     loadFile = new FileReference(); 

     loadFile.addEventListener(Event.SELECT, selectHandler); 
     var fileFilter:FileFilter = new FileFilter("Images: (*.jpeg, *.jpg, *.gif, *.png)", "*.jpeg; *.jpg; *.gif; *.png"); 
     loadFile.browse([fileFilter]); 
    } 

    private function selectHandler(event:Event):void 
    { 
     loadFile.load();  
    } 


     [Bindable] 
     public var img:ByteArray; 
     public function storeInputs(event:MouseEvent):void 
     { 
      if (employeeImageName.text.length == 0) 
      { 
       loadFile = new FileReference(); 
       url = new URLRequest("http://localhost/images/male.jpg"); 
        loadFile.addEventListener(Event.COMPLETE, function (event:Event):void 
        { 
         loadFile.upload(url); 
        }); 
       } 
       img = loadFile.data;     
       arrayCollection.addItem({ 
           name : employeeName.text, 
           imageData: img 
           }); 
     } 

此代碼工作完全當我選擇使用瀏覽按鈕的圖像文件。但是,當我提交表單而未選擇任何圖像文件時,數據將返回空數據。任何人都可以找到我的錯誤?

回答

0

可能是這個代碼可以幫助您......因爲在你的代碼......不叫完整的事件......當上傳然後只完成了所謂的...

if (employeeImageName.text.length == 0) 
{ 
    loadFile =new FileReference(); 

    var url:URLRequest = new URLRequest("http://localhost/images/male.jpg"); 

    loadFile.addEventListener(Event.COMPLETE, function (event:Event):void 

    { 
    img = loadFile.data; 

    }); 

    loadFile.upload(url); 

} 
+0

我已經嘗試過了。但沒有效果。 – user

+0

對於遲到的回覆感到抱歉...可能是這2個鏈接幫助你... http://askjayvir.blogspot.in/2008/07/sample-flex-file-upload-application.html和http:// help。 adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html – Jiya1491