智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

文件上传下载小例

Posted on 2009-12-01 09:50  Bill Yuan  阅读(315)  评论(0编辑  收藏  举报
代码
//文件上传
package
{
    
import flash.net.FileReference;
    
import flash.events.Event;
    
import flash.net.URLRequest;

    
public class UploadExample
    {
        
public function UploadExample()
        {
            var fileRef:FileReference 
= new FileReference();
            fileRef.addEventLister(Event.SELECT, selectHandler);
            fileRef.addEventLister(Event.COMPLETE, completeHandler);
            
try
            {
                var success:Boolean 
= fileRef.browse();
            }
            
catch (error:Rrror)
            {
                trace(
"不能浏览文件.");
            }
        }

        function selectHandler(event:Event):
void
        {
            var request:URLRequest 
= new URLRequest("");
            
try
            {
                fileRef.upload(request);
            }
            
catch (error:Error)
            {
                trace(
"不能上传文件.");
            }
        }

        function completeHandler(event:Event):
void
        {
            trace(
"上传文件完成.");
        }
    }
}
//文件下载
package
{
    
import flash.display.Sprite;
    
import flash.net.FileReference;
    
import flash.net.URLRequest;
    
import flash.net.URLRequestMethod;
    
import flash.net.URLVariables;

    
public class DownloadFileExample extends Sprite
    {
        
private var fileToDownload:FileReference;

        
public function DownloadFileExample()
        {
            var request:URLRequest 
= new URLRequest();
            request.url 
= "";
            request.method 
= URLRequestMethod.GET();
            request.data 
= new URLVariables("id=2");
            fileToDownload 
= new FileReference();
            
try
            {
                fileToDownload.download(request, 
"file.txt");
            }
            
catch (error:Error)
            {
                trace(
"不能下载数据!");
            }
        }
    }
}