我想创建多个文件上传Flash应用。我需要知道如何同时跟踪多个进度条的多个文件上传。
答案 0 :(得分:1)
这是演示。 主要思想是创建一个包含FileReference或URLLoader以及进度指示器的类。
Main.as:
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
var btnText:TextField = new TextField;
btnText.autoSize = TextFieldAutoSize.LEFT;
btnText.text = "Click to upload";
var btn:SimpleButton = new SimpleButton(btnText, btnText, btnText, btnText);
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, onBtnClick);
}
private function onBtnClick(e:MouseEvent):void
{
var uploader:Uploader = new Uploader("http://www.yahoo.com/");
uploader.y = this.height;
addChild(uploader);
}
}
}
Uploader.as:
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.*;
public class Uploader extends Sprite
{
private var url:String;
private var fileRef:FileReference;
private var text:TextField = new TextField;
public function Uploader(url:String )
{
this.url = url;
text.autoSize = TextFieldAutoSize.LEFT;
text.text = "..."
addChild(text);
fileRef = new FileReference();
fileRef.browse();
fileRef.addEventListener(Event.SELECT, onSelect);
}
private function onSelect(e:Event):void
{
text.text = fileRef.name + " : starting upload";
var req:URLRequest = new URLRequest(url);
req.method = URLRequestMethod.POST;
fileRef.upload(req, fileRef.name);
fileRef.addEventListener(ProgressEvent.PROGRESS, onProgress);
fileRef.addEventListener(Event.COMPLETE, onComplete);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, onError);
}
private function onError(e:IOErrorEvent):void
{
text.text = fileRef.name + " :" + e.text;
}
private function onComplete(e:Event):void
{
text.text = fileRef.name + " : Complete";
}
private function onProgress(e:ProgressEvent):void
{
text.text = fileRef.name + " : " + e.bytesLoaded + "/" + e.bytesTotal;
}
}
}
答案 1 :(得分:0)
创建一个URLLoader并收听PROGRESS事件。