private function initMask():void
{
_mask = new Sprite();
_mask.graphics.beginFill(0x000000);
_mask.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
_mask.graphics.endFill();
_mask.alpha = 0.6;
_info = new TextField();
_info.text = "图片正在上传中...";
var tf:TextFormat = new TextFormat("宋体",16,0xff0000);
_info.setTextFormat(tf);
_info.x = (stage.stageWidth - _info.textWidth) / 2;
_info.y = (stage.stageHeight - _info.textHeight) / 2;
_info.autoSize = TextFieldAutoSize.CENTER;
}
private function initDesc():void
{
var desc:TextField = new TextField();
desc.text = "copy right for 谢承雄.";
var tf:TextFormat = new TextFormat("宋体",12,0xff0000);
desc.setTextFormat(tf);
desc.x = stage.stageWidth - desc.textWidth;
desc.y = stage.stageHeight - desc.textHeight-10;
desc.autoSize = TextFieldAutoSize.CENTER;
addChild(desc);
}
private function addScrollBar():void
{
_scrollBar = new Sprite();
_scrollBar.graphics.beginFill(0x000000);
_scrollBar.graphics.drawRect(((5 * _width) + 30),2,10,30);
_scrollBar.graphics.endFill();
_scrollBar.addEventListener(MouseEvent.MOUSE_DOWN,_down);
_scrollBar.addEventListener(MouseEvent.MOUSE_UP,_up);
}
private function _down(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE,_move);
stage.addEventListener(MouseEvent.MOUSE_UP,_up);
}
private function _up(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE,_move);
stage.removeEventListener(MouseEvent.MOUSE_UP,_up);
}
private function _move(event:MouseEvent):void
{
if (((mouseY > 0) && mouseY < _container.height - 30))
{
setScrollPos(mouseY);
}
}
private function setScrollPos(y:Number):void
{
var height:Number = Math.ceil((_list.fileList.length + 1) / 5) * 105 + 5 - _container.height;
var rect:Rectangle = _container.scrollRect;
rect.y = height * ((y - 5) / (_container.height - 35));
_scrollBar.y = y;
_container.scrollRect = rect;
}
public function upload():void
{
if (_list.validList.length > 0)
{
_index = 0;
addChild(_mask);
addChild(_info);
uploadBatch(0);
}
}
private function uploadBatch(i:Number):void
{
if (Config.START_UPLOAD_CALLBACK && ExternalInterface.available)
{
ExternalInterface.call(Config.START_UPLOAD_CALLBACK, _list.validList[i]);
}
var jpegEncoder:JPGEncoder = new JPGEncoder(_loader.width);
var jpegBytes:ByteArray = jpegEncoder.encode(_list.bitmapList[i]);
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = Config.UPLOAD_URL;
urlRequest.method = URLRequestMethod.POST;
var params:Object = {};
urlRequest.data = UploadPostHelper.getPostData(_list.validList[i].name,jpegBytes,Config.FIELD_NAME,params);
urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control','no-cache'));
urlRequest.requestHeaders.push(new URLRequestHeader('Content-Type','multipart/form-data; boundary=' + UploadPostHelper.getBoundary()));
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE,uploadComlete);
urlLoader.load(urlRequest);
}
private function uploadComlete(evt:Event):void
{
if (Config.UPLOAD_COMPLETE_CALLBACK && ExternalInterface.available)
{
ExternalInterface.call(Config.UPLOAD_COMPLETE_CALLBACK, _list.validList[_index]);
}
_index++;
if ((_index < _list.validList.length))
{
_info.text = "当前正在上传第" + (_index + 1) + "/" + _list.validList.length + "张图片...";
uploadBatch(_index);
}
else
{
_info.text = "图片全部上传完成!";
if (Config.ALL_COMPLETE_CALLBACK && ExternalInterface.available)
{
ExternalInterface.call(Config.ALL_COMPLETE_CALLBACK, _list.validList);
}
}
var tf:TextFormat = new TextFormat("宋体",16,0xff0000);
_info.setTextFormat(tf);
}
private function selectFile(evt:MouseEvent):void
{
if (! _fileReferenceList)
{
_fileReferenceList = new FileReferenceList();
_fileReferenceList.addEventListener(Event.SELECT,selectHandler);
_fileReferenceList.addEventListener(Event.COMPLETE,selectCompleteHandler);
}
_fileReferenceList.browse();
}
private function selectHandler(evt:Event):void
{
ExternalInterface.call("console.dir",evt.target.fileList);
_index = 0;
_pendingFileList = evt.target.fileList;
_list.fileList = _list.fileList.concat(_pendingFileList);
addPendingFile(_pendingFileList[0]);
}
private function addPendingFile(file:FileReference):void
{
if (/(.jpeg|.jpg|.png|.gif|.bmp)$/g.test(file.type))
{
_list.validList.push(file);
file.addEventListener(Event.COMPLETE,selectCompleteHandler);
if (! _loader)
{
_loader = new Loader();
}
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadHandler);
file.load();
}
else
{
addError(_list.itemList.length, "格式错误!");
loadCompelete();
}
}
private function selectCompleteHandler(evt:Event):void
{
_loader.loadBytes(evt.target.data);
}
private function loadHandler(evt:Event):void
{
addItem(_list.itemList.length);
loadCompelete();
}
private function loadCompelete():void
{
_index++;
if ((_index < _pendingFileList.length))
{
addPendingFile(_pendingFileList[_index]);
}
if ((_index == _pendingFileList.length))
{
if (Config.SELECT_FILE_CALLBACK && ExternalInterface.available)
{
ExternalInterface.call(Config.SELECT_FILE_CALLBACK, _list.validList);
}
if (_list.fileList.length >= 15)
{
! stage.contains(_scrollBar) && addChild(_scrollBar);
setScrollPos(_container.height - 30);
}
else
{
stage.contains(_scrollBar) && removeChild(_scrollBar);
}
}
}
private function addPlusItem():void
{
_plusItem = new Sprite();
var x:Number = 5;
var y:Number = 5;
_plusItem.graphics.drawRect(x,y,_width,_height);
_plusItem.graphics.beginFill(0xefefef,1);
_plusItem.graphics.lineStyle(1,0x000000,1);
_plusItem.graphics.moveTo((x - 1),y - 1);
_plusItem.graphics.lineTo(((x + _width) + 1),y - 1);
_plusItem.graphics.lineTo(((x + _width) + 1),y + _height + 1);
_plusItem.graphics.lineTo((x - 1),y + _height + 1);
_plusItem.graphics.lineTo((x - 1),y - 1);
_plusItem.graphics.endFill();
_container.addChild(_plusItem);
var btnText:TextField = new TextField ;
btnText.text = "添加图片";
var tf:TextFormat = new TextFormat("宋体",16,0x000000);
btnText.setTextFormat(tf);
btnText.height = btnText.textHeight;
btnText.x = x;
btnText.y = y + (_height - btnText.height) / 2;
btnText.autoSize = TextFieldAutoSize.CENTER;
_plusItem.addChild(btnText);
_plusItem.addEventListener(MouseEvent.CLICK,selectFile);
}
private function movePlusItem(i:Number):void
{
_plusItem.x = (_width + 5) * int((i % 5));
_plusItem.y = (_height + 5) * int((i / 5));
}
|