欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fileReference 的运用

程序员文章站 2022-05-07 16:45:44
...
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				layout="vertical"
				verticalAlign="middle"
				backgroundColor="white"
				creationComplete="init();">
	
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private var fileRef:FileReference;
			
//			private const FILE_UPLOAD_URL:String = "http://www.YOUR-WEBSITE-HERE.com/fileref/uploader.cfm";
			
			private function init():void {
				fileRef = new FileReference();
				fileRef.addEventListener(Event.SELECT, fileRef_select);
				fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
				fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
			}
			
			private function browseAndUpload():void {
				fileRef.browse();
				message.text = "";
			}
			
			private function fileRef_select(evt:Event):void {
				try {
					//message.text = "size (bytes): " + numberFormatter.format(fileRef.size);
					fileRef.load();
				} catch (err:Error) {
					message.text = "ERROR: zero-byte file";
				}
			}
			
			private function fileRef_progress(evt:ProgressEvent):void {
				progressBar.visible = true;
			}
			
			private function fileRef_complete(evt:Event):void {
				message.text += " (complete)";
				var fileExtensionName:String = fileRef.name.substring(fileRef.name.lastIndexOf('.'));
				if(fileExtensionName.toLowerCase() != ".txt")
				{
					Alert.show("The File you are try to Upload is not .txt file!");
				}
				progressBar.visible = false;
			}
		]]>
	</mx:Script>
	
	<mx:NumberFormatter id="numberFormatter" />
	
	<mx:Button label="Upload file"
			   click="browseAndUpload();" />
	<mx:Label id="message" />
	<mx:ProgressBar id="progressBar"
					indeterminate="true"
					visible="false" />
	
</mx:Application>