In Flex application development, like asp.net,jsp,php applications, there will be upload/download file application requirements, Flex SDK also provides us with a special class Filerefudderence implementation file upload/download. Flex is only as a client, to achieve upload or download must provide a service to accept the upload or download request, this article in asp.net httphandler as a file upload server to complete the upload function.
OK, let's start with the flex client and see how the client wants the server to initiate the request. Flex client to complete file upload download is through the filerefudderence to achieve, first of all to define a type of object instance:
1 [Bindable]
2 private var stateText:String = "请选择一个文件上传";
3 //通过调用file对象的方法来完成上传和下载 功能
4 private var file:FileReference = new FileReference();
Uploading files usually involves selecting files, uploading files, and uploading them to complete these most basic processes. OK, let's take these three procedures as an example to see how flex is to complete the file upload function. First, add a listener event handler for each of the three feature points, which is called when the program loads:
1 internal function initApp ():void
2 {
3 file.addEventListener(Event.SELECT,onSelected);
4 file.addEventListener (Event.COMPLETE,onCompleted);
5 file.addEventListener(ProgressEvent.PROGRESS,onProgress);
6 }
In addition, we can also use the above to define a function to invoke the initialization operation when the program is loaded, the initialization of the application (Mxml) is Creationcomplete and the method completes, and there is a method Createchildren () that executes it first. We can override this method directly under Mxml to implement initialization of the application, as follows:
1 /**
2 * createChildren 比 creationComplete 事件更早发生
3 * */
4 protected override function createChildren ():void
5 {
6 file.addEventListener(Event.SELECT,onSelected);
7 file.addEventListener (Event.COMPLETE,onCompleted);
8 file.addEventListener(ProgressEvent.PROGRESS,onProgress);
9 }
The detailed definitions of these three event handlers are as follows (Statetext is a string variable that displays the file upload status hint):
1 internal function onSelected(evt:Event):void
2 {
3 stateText = "选择了文件" + file.name;
4 }
5
6 internal function onCompleted(evt:Event):void
7 {
8 stateText = "上传完毕!";
9 }
10
11 internal function onProgress(evt:ProgressEvent):void
12 {
13 stateText = "已上传 " + Math.round(100 * evt.bytesLoaded / evt.bytesTotal) + "%";
14 }
Here the client is only one step, that is, the completion of the launch of the upload request method, in fact, through the URLRequest object to create a connection with the server, and then directly call the Fielreference class upload () method to complete the function, the following code definition:
1 /**
2 * 调用FileReference的实例方法upload()实 现文件上传
3 * */
4 internal function onUpLoad():void
5 {
6 if(file.size > 0)
7 {
8 stateText = "正在上传文件:" + file.name;
9 }
10 var request:URLRequest = new URLRequest();
11 request.url="http://localhost/Web/UpLoadHandler.ashx";
12 file.upload(request);
13 }