代码
//文件上传
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("不能下载数据!");
}
}
}
}
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("不能下载数据!");
}
}
}
}