为什么在我第二次点击 fileReferencer.browse 按钮后 TextInput 会更新
在 Flex 4 中,我试图使一个简单的浏览按钮使用浏览按钮获取的文件名更新 TextInput 对象的文本字段。它不必是完整路径,我想要的只是显示的文件名。它仅在第二次点击浏览按钮后显示,而不是在我第一次选择文件后显示。这是我的代码:
import flash.net.FileReference;
private var fileReferencer:FileReference = new FileReference();
private var excelFilter:FileFilter = new FileFilter("*.xlsx", "*.xlsx;*.xls;");
protected var fileName:String = new String("");
protected function BrowseButton_clickHandler(event:MouseEvent):void
{
fileReferencer.browse([excelFilter]);
fileName = fileReferencer.name;
fileInputAddress.text = fileName;
}
回顾一下,文件名仅在第二次点击浏览按钮时显示在我的 TextInput 框中。
我做错了什么?
In Flex 4, I am trying to make a simple browse button update the text field of an TextInput object with the file name that the browse button gets. It doesn't have to be the full path, all I want is the file name to show up. It only shows up after hitting the browse button for a second time, not after I have selected my file the first time. Here is my code:
import flash.net.FileReference;
private var fileReferencer:FileReference = new FileReference();
private var excelFilter:FileFilter = new FileFilter("*.xlsx", "*.xlsx;*.xls;");
protected var fileName:String = new String("");
protected function BrowseButton_clickHandler(event:MouseEvent):void
{
fileReferencer.browse([excelFilter]);
fileName = fileReferencer.name;
fileInputAddress.text = fileName;
}
So to recap, the file name is only shown in my TextInput box upon hitting the browse button a second time.
What am I doing wrong?
Flash Player 是完全异步的。因此,调用
fileReferencer.browse()
后无法立即获取文件名。这就是为什么你有过去通话中的名字。要修复代码,您应该订阅select
和cancel
事件,并仅在select
事件之后更改文本(请参阅 文档)。Flash Player is completely asynchronous. So you can't get file name right after calling
fileReferencer.browse()
. That's why you have a name from the past call. To fix your code you should subscribe onselect
andcancel
events and change the text afterselect
event only (see the documentation).