0

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?

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
crstamps2
  • 606
  • 2
  • 6
  • 16

1 Answers1

1

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 on select and cancel events and change the text after select event only (see the documentation).

Constantiner
  • 14,231
  • 4
  • 27
  • 34
  • Just as you were answering this I found a blog post that uses your exact answer. Thank you so much for your help. If anyone is interested in that link, it is [here](http://dgrigg.com/blog/2007/08/02/Flex-and-Flash-file-uploading-with-return-data/) – crstamps2 Jun 20 '11 at 14:59