在有些应用中,比如上传,下载的时候,往往都只是允许一部分类型的文件可以上传或者下载。下面的代码演示了Flex中创建一个可以过滤特定后缀文件的FileReference
1 创建一个FileFilter实例并传递到FileReference的一般方法,代码如下:
- var p_w_picpathsFilter:FileFilter = new FileFilter("Images",
- "*.jpg;*.gif;*.png");
- var docFilter:FileFilter = new FileFilter("Documents",
- "*.pdf;*.doc;*.txt");
- var myFileReference:FileReference = new FileReference();
- myFileReference.browse([p_w_picpathsFilter, docFilter]);
2 完整示例(点击按钮,只显示pdf后缀的文件):
- <?xml version="1.0"?>
- <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
- xmlns:s="library://ns.adobe.com/flex/spark"
- xmlns:mx="library://ns.adobe.com/flex/mx">
- <fx:Script>
- <![CDATA[
- private var oneFileFileFilter:FileFilter = new
- FileFilter("sample.pdf", "sample.pdf");
- private var pdfFileFileFilter:FileFilter = new
- FileFilter("PDF Files", "*.pdf");
- private function oneFile():void{
- var fileRef:FileReference = new FileReference();
- fileRef.browse([oneFileFileFilter]);
- }
- private function pdfFiles():void{
- var fileRef:FileReference = new FileReference();
- fileRef.browse([pdfFileFileFilter]);
- }
- ]]>
- </fx:Script>
- <mx:Button label="One File - sample.pdf"
- click="oneFile()"/>
- <mx:Button label="All pdf Files" click="pdfFiles()"/>
- </s:Application>