AS3和AMFPHP上传文件到服务器

    最近在做一个项目,需要用到AS3把图片上传到服务器上面。服务器语言选择PHP。于是,在网上搜索了一下,发现主流的操作是利用AS3+AMFPHP进行结合。
      纠结了半天,写了一个客户端DEMO和服务器端脚本DEMO。
      客户端三个辅助类:
      package 
{
      import com.adobe.images.JPGEncoder;
      import flash.display.Sprite;
      import flash.net.FileFilter;
      import flash.display.Sprite;
      import flash.display.Bitmap;
      import flash.display.BitmapData;
      import flash.geom.Matrix;
      import flash.net.FileReference;
      import flash.events.Event;
      import flash.display.Loader;
      import flash.utils.ByteArray;
     
     
      public class LoadBmp extends Sprite
      {
            public var smallW:Number;//
            public var smallH:Number;
            public var bitmap:Bitmap;
            private var file:FileReference;
            private var loaders:Loader;
            private var com:Function;
            public function LoadBmp(smallW:uint = 120, smallH:uint = 120)
            {
                  this.bitmap = new Bitmap();
                  this.file = new FileReference();
                  this.loaders = new Loader();
                  this.smallW = smallW;
                  this.smallH = smallH;
            }
            private function small(smallW:Number, smallH:Number, bitmapdata:BitmapData):BitmapData
            {
                  var xx:Number = smallW / bitmapdata.width;
                  var yy:Number = smallH / bitmapdata.height;
                  var ww:Number = (smallW - bitmapdata.width * xx) / 2;
                  var hh:Number = (smallH - bitmapdata.height * yy) / 2;
                  var matrix:Matrix = new Matrix();
                  matrix.scale(xx, yy);
                  matrix.translate(ww, hh);
                  var bmpdata:BitmapData = new BitmapData(smallW, smallH, false, 268435455);
                  bmpdata.draw(bitmapdata, matrix);
                  return bmpdata;
            }
            public function loads(callback:Function):void
            {
                  this.file.browse([new FileFilter("只能图片啊", "*.jpg;*.png;*.gif")]);
                  this.file.addEventListener(Event.SELECT, this.onSelect);
                  com = callback;
                  return;
            }
            private function onSelect(e:Event):void
            {
                  this.file.load();
                  this.file.addEventListener(Event.COMPLETE, this.onComplete);
            }
            private function onComplete(e:Event):void {
                  this.loaders.loadBytes(this.file.data);
                  this.loaders.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onComplete2);
                  return;
            }
            private function onComplete2(e:Event):void
            {
                  this.bitmap = e.target.content as Bitmap;
                  this.bitmap.bitmapData = this.small(this.smallW, this.smallH, this.bitmap.bitmapData);
                  this.bitmap.x = 0;
                  addChild(this.bitmap);
                  com();
            }
     
      }

}

        package 
{
      import flash.display.SimpleButton;
     
     
      public class CustomSimpleButton extends SimpleButton {
      private var upColor:uint    = 0xFFCC00;
      private var overColor:uint = 0xCCFF00;
      private var downColor:uint = 0x00CCFF;
      private var size:uint          = 80;

      public function CustomSimpleButton() {
              downState          = new ButtonDisplayState(downColor, size);
              overState          = new ButtonDisplayState(overColor, size);
              upState              = new ButtonDisplayState(upColor, size);
              hitTestState    = new ButtonDisplayState(upColor, size * 2);
              hitTestState.x = -(size / 4);
              hitTestState.y = hitTestState.x;
              useHandCursor  = true;
      }
}

}

package 
{
      import flash.display.Shape;
     
     
      public class ButtonDisplayState extends Shape {
      private var bgColor:uint;
      private var size:uint;

      public function ButtonDisplayState(bgColor:uint, size:uint) {
              this.bgColor = bgColor;
              this.size      = size;
              draw();
      }

      private function draw():void {
              graphics.beginFill(bgColor);
              graphics.drawRect(0, 0, size, size);
              graphics.endFill();
      }
}

}
Main类:
package
{
      import flash.display.SimpleButton;
      import flash.display.Sprite;
      import flash.events.Event;
      import flash.events.MouseEvent;
      import flash.net.NetConnection;
      import flash.display.BitmapData;
      import flash.display.Bitmap;
      import flash.text.TextField;
      import flash.utils.ByteArray;
      import com.adobe.images.JPGEncoder;
      import flash.net.Responder;
     
     
      public class Main extends Sprite
      {
            private var lb:LoadBmp;
            private var connect:NetConnection;
            public var resultTxt:TextField;
            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);
                  // entry point
                  var simpleBtton:CustomSimpleButton = new CustomSimpleButton();
     
                  lb = new LoadBmp();
                  resultTxt = new TextField();
                  addChild(resultTxt);
                  resultTxt.width = resultTxt.height = 300;
                  resultTxt.x = resultTxt.y = 200;
                  resultTxt.text = "4544";
                  simpleBtton.addEventListener(MouseEvent.CLICK, handleClick);
                  stage.addChild(simpleBtton);
            }
            private function handleClick(mevt:MouseEvent):void {
                  lb.loads(callback);
            }
            public function callback():void {
                  connect = new NetConnection();
                  connect.connect("../../../Examples/Php/");
                  connect.call("PizzaService/createSaveJpgFile", new Responder(handleResult, null),getBytes(lb.bitmap.bitmapData));
                  //connect.call("Counter.createSaveJpgFile", null, getBytes(lb.bitmap.bitmapData));
            }
            private function handleResult(result:Object):void {
                  resultTxt.text = result.toString();
            }
            public function getBytes(bmpData:BitmapData):ByteArray
            {
                  var encoder:JPGEncoder = new JPGEncoder(90);
                  var bytes:ByteArray = encoder.encode(bmpData);
                  return bytes;
            }
      }
     





     
}

PHP端的service函数:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值