这里直接是取主场景上的BitmapData,场景上的任何对象将最终转为图片,值得注意的是stage.width为场景上绘制对象的宽,而非整个场景的宽,PNGEncoder是来自adobe的官方的类库,可将图片转为BitmapData,它支持转jpg格式,并可以设置图片精度,不过png支持透明,颜色更棒!你可以在http://code.google.com/p/as3corelib/下载到这个类库:corelib ActionScript 3 Library,我的程序就用到了com.adobe.images下的3个类BitString.as、JPGEncoder.as、PNGEncoder.as
#
import com.adobe.images.*;
#
import flash.net.*;
#
import flash.display.*;
#
import flash.utils.ByteArray;
#
var myBitmapData:BitmapData=new BitmapData(stage.width,stage.height,true,0);
#
myBitmapData.draw(stage);
#
//var jpegEnc:JPGEncoder= new JPGEncoder(100);
#
//var dat:ByteArray = jpegEnc.encode(myBitmapData);
#
var dat:ByteArray = PNGEncoder.encode(myBitmapData);
#
var req:URLRequest = new URLRequest( "http://localhost/img/psavepic.php");
#
req.data = dat;
#
req.method = URLRequestMethod.POST;
#
req.contentType = 'application/octet-stream ';
#
var loader:URLLoader = new URLLoader();
#
loader.addEventListener(Event.COMPLETE, completeHandler);
#
loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
#
function completeHandler(event:Event):void{
#
trace("图片上传成功");
#
}
#
function errorHandler(event:IOErrorEvent):void{
#
trace("图片上传失败");
#
}
#
loader.dataFormat = URLLoaderDataFormat.BINARY;
#
loader.load(req);
savepic.php获得从flash传过来的图片数据,写入到文件,数据有点特别,所以$_REQUEST是取不到,要用$HTTP_RAW_POST_DATA,当然也可以用jsp、asp、asp.net来实现,不过就没有php的代码简单高效了!i like php!
1.
<?php
2.
if(isset($HTTP_RAW_POST_DATA)){
3.
$name =time().'.jpg';
4.
$im = fopen($name,'a');
5.
fwrite($im,$HTTP_RAW_POST_DATA);
6.
fclose($im);
7.
}
8.
?>
public function SaveFlashJpgOrGif(array $data)
{
$input = $this->parseFormParams($data['input']);
$qq = ".".$input['FQQ'];
$uploadPicKinds = ".".$input['uploadPicKinds'];
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
}else
{
//failed
echo "HTTP_RAW_POST_DATA is Empty";
exit;
}
$path = ROOT_PATH."view/data/";
$fileName = $qq.date("Y_m_d_H_i_s").$uploadPicKinds; //QQ+date
$filename=$path.$fileName;
file_put_contents($filename,$jpg);
$this->set(__CLASS__."_".__FUNCTION__,$fileName);
}
来源:http://hi.baidu.com/jhmis/blog/item/68a0fd2ab89567f2e7cd4030.html
继续参考:http://www.04js.cn/content.asp?id=1097
调试另外一种post的上传方式:首先是打印$_FILE后去接受变量即可!
#
import com.adobe.images.*;
#
import flash.net.*;
#
import flash.display.*;
#
import flash.utils.ByteArray;
#
var myBitmapData:BitmapData=new BitmapData(stage.width,stage.height,true,0);
#
myBitmapData.draw(stage);
#
//var jpegEnc:JPGEncoder= new JPGEncoder(100);
#
//var dat:ByteArray = jpegEnc.encode(myBitmapData);
#
var dat:ByteArray = PNGEncoder.encode(myBitmapData);
#
var req:URLRequest = new URLRequest( "http://localhost/img/psavepic.php");
#
req.data = dat;
#
req.method = URLRequestMethod.POST;
#
req.contentType = 'application/octet-stream ';
#
var loader:URLLoader = new URLLoader();
#
loader.addEventListener(Event.COMPLETE, completeHandler);
#
loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
#
function completeHandler(event:Event):void{
#
trace("图片上传成功");
#
}
#
function errorHandler(event:IOErrorEvent):void{
#
trace("图片上传失败");
#
}
#
loader.dataFormat = URLLoaderDataFormat.BINARY;
#
loader.load(req);
savepic.php获得从flash传过来的图片数据,写入到文件,数据有点特别,所以$_REQUEST是取不到,要用$HTTP_RAW_POST_DATA,当然也可以用jsp、asp、asp.net来实现,不过就没有php的代码简单高效了!i like php!
1.
<?php
2.
if(isset($HTTP_RAW_POST_DATA)){
3.
$name =time().'.jpg';
4.
$im = fopen($name,'a');
5.
fwrite($im,$HTTP_RAW_POST_DATA);
6.
fclose($im);
7.
}
8.
?>
public function SaveFlashJpgOrGif(array $data)
{
$input = $this->parseFormParams($data['input']);
$qq = ".".$input['FQQ'];
$uploadPicKinds = ".".$input['uploadPicKinds'];
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
}else
{
//failed
echo "HTTP_RAW_POST_DATA is Empty";
exit;
}
$path = ROOT_PATH."view/data/";
$fileName = $qq.date("Y_m_d_H_i_s").$uploadPicKinds; //QQ+date
$filename=$path.$fileName;
file_put_contents($filename,$jpg);
$this->set(__CLASS__."_".__FUNCTION__,$fileName);
}
来源:http://hi.baidu.com/jhmis/blog/item/68a0fd2ab89567f2e7cd4030.html
继续参考:http://www.04js.cn/content.asp?id=1097
调试另外一种post的上传方式:首先是打印$_FILE后去接受变量即可!
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/3292/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2010-7-14 18:48
评论列表