主类 HTTPSendPHP.as
package
{
import com.JPEGEncoder.JPGEncoder;
import com.fylib.httpRequest.HttpRequestBuilder;
import com.fylib.httpRequest.HttpRequestBuilderConsts;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.ByteArray; /**
* ...
* @author Frost.Yen
*/
public class HTTPSendPHP extends Sprite
{
private var bmp:Bitmap;
public function HTTPSendPHP()
{
var ldr:Loader = new Loader ;
ldr.load(new URLRequest("image.jpg"));
//加载的图片;
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
} private function onComplete(e:Event):void
{
var bmpd:BitmapData = e.target.content as Bitmap.bitmapData;
var jpgEncoder:JPGEncoder = new JPGEncoder(80);//图片质量
var jpgStream:ByteArray = jpgEncoder.encode(bmpd);//将图片数据转换成ByteArray存储 var HttpR:HttpRequestBuilder = new HttpRequestBuilder("php端地址");//php端地址
HttpR.writeImage("图片变量","图片名称",HttpRequestBuilderConsts.JPG,jpgStream);
//图片变量如:"data[Image][name]";
HttpR.writeHttpEnd();
HttpR.writeSeparator(); var loader:URLLoader = new URLLoader ;
var request:URLRequest = HttpR.getURLrequest();
loader.load(request);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler);
loader.addEventListener(Event.COMPLETE,onSendComplete);
} private function onSendComplete(e:Event):void
{
trace("complete");
} private function httpStatusHandler(e:HTTPStatusEvent):void
{
trace(("status is " + e.status));
}
} }
其他类
package com.fylib.httpRequest
{
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.utils.ByteArray; /**
* ...
* @author
*/
public class HttpRequestBuilder
{
/**
* 构造的URLRequest
*/
private var _request:URLRequest;
/**
* 构造的二进制ByteArray
*/
private var _byteArray:ByteArray;
/**
* Http协议分割符
*/
private var _separator:String;
/**
* 标志位 是否填写HTTP文件尾
*/
private var PROTOCAL_END_SET:Boolean = false; public function HttpRequestBuilder($url:String,$separator:String="7d86d710144a")
{
//初始化
this._separator = $separator;
this._request = new URLRequest($url);
this._request.method = URLRequestMethod.POST;
this._request.contentType = "multipart/form-data; boundary=---------------------------" + this._separator;
this._byteArray = new ByteArray ;
} /*
* 写入变量
* @param $name 写入的变量名
* @param $value 写入的变量值
*/
public function writeVariable($name:String,$value:String):void
{
writeSeparator();
_byteArray.writeUTFBytes(((("Content-Disposition: form-data; name=\"" + $name) + "\"\r\n\r\n") + $value));
} /*
* 写入图片
* @param $name 变量名
* @param $filename 图片文件名
* @param $type 图片类型,在HttpRequestBuilderConsts下定义
* @param $content 图片二进制数据
*/
public function writeImage($name:String,$filename:String,$type:String,$content:ByteArray):void
{
writeSeparator();
_byteArray.writeUTFBytes((((((("Content-Disposition: form-data; name=\"" + $name) + "\"; filename=\"") + $filename) + "\"\r\nContent-Type: ") + $type) + "\r\n\r\n"));
_byteArray.writeBytes($content);
} /*
* 构造HTTP分割线
*/
public function writeSeparator():void
{
_byteArray.writeUTFBytes((("\r\n-----------------------------" + separator) + "\r\n"));
} /*
* 构造HTTP结尾,只能调用一次,二次调用会抛出错误
*/
public function writeHttpEnd():void
{
if (! PROTOCAL_END_SET)
{
_byteArray.writeUTFBytes((("\r\n-----------------------------" + separator) + "--"));
PROTOCAL_END_SET = true;
}
else
{
throw new Error("Write the Http End More Than Once");
}
} /*
* 获取构造好的URLRequest
*/
public function getURLrequest():URLRequest
{
return this.request;
} //get set
public function get separator():String
{
return _separator;
}
/*public function set separator(value:String):void
{
//TODO 替换之前写入的内容
_separator = value;
}*/ /*
* 获取前会检查是否写入HTTP结尾,未调用的话会报错
*/
public function get request():URLRequest
{
if (! PROTOCAL_END_SET)
{
throw new Error("Havn't Write the Http End Yet");
}//??还是应该自动构造
_request.data = _byteArray;
return _request;
}
} }
package com.fylib.httpRequest
{
/**
* ...
* @author
*/
public class HttpRequestBuilderConsts
{
public static const JPG:String = "image/jpeg";
public static const PNG:String = "image/x-png"; public function HttpRequestBuilderConsts()
{ } } }