Flex 4 - FileReference数据返回null值

时间:2014-03-19 07:00:33

标签: actionscript-3 flex flex4

我有一个 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
                                 });
        }

当我使用浏览按钮选择图像文件时,此代码工作正常。但是,当我提交表单而不选择任何图像文件时, FileReference 数据将返回空数据。任何人都可以找到我的错误?

1 个答案:

答案 0 :(得分: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);

}