Error #2044 when trying to save image from Flash using php
I get this error in the debugger version of flash Player Error #2044: Unhandled ioError:. text=Error #2032: Stream Error
i searched the net for an explanation without success
the code below works in WampServer http://localhost/ but not when i put it online on the server:
as3 :
import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;
var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height);
jpgSource.draw(img_mc);
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
//set the request's header,method and data
var header:URLRequestHeader = new URLRequestHeader("Content-type","application/octet-stream");
var loader:URLLoader = new URLLoader();
//sends jpg bytes to saveJPG.php script
var myRequest:URLRequest = new URLRequest("saveJPG.php");
myR开发者_如何学Pythonequest.requestHeaders.push(header);
myRequest.method = URLRequestMethod.POST;
myRequest.data = jpgStream;
loader.load(myRequest);
//fire complete event;
loader.addEventListener(Event.COMPLETE,saved);
function saved(e:Event)
{
//trace the image file name
trace(loader.data);
}
saveJPG.php:
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
//the image file name
$fileName = 'img.jpg';
// get the binary stream
$im = $GLOBALS["HTTP_RAW_POST_DATA"];
//write it
$fp = fopen($fileName, 'wb');
fwrite($fp, $im);
fclose($fp);
//echo the fileName;
echo $fileName;
} else echo 'result=An error occured.';
update:
i tried $im = file_get_contents("php://input");
instead of $im = $GLOBALS["HTTP_RAW_POST_DATA"];
and i got the same error.
var myRequest:URLRequest = new URLRequest("saveJPG.php");
Try to set complete url for request.
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error
Do what it wants add the unhandled ioError eventlistener
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler );
function ioErrorHandler( e:Event ):void{
trace('ioErrorHandler ' + e.toString() )
}
And you should have this one also
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
function securityErrorHandler ( e:Event ):void{
trace('securityErrorHandler ' + e.toString() )
}
it seems that the server is preventing me from exchanging bytes or using $GLOBALS["HTTP_RAW_POST_DATA"];
or something like that.
so i come up with different approach, instead of sending the image as bytes to php file i'll send it as hexadecimal string:
import com.adobe.images.JPGEncoder;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequestHeader;
import flash.net.URLRequest;
var jpgSource:BitmapData = new BitmapData(img_mc.width,img_mc.height);
jpgSource.draw(img_mc);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);
//get the whole jpgStream as a string of hex
var hexStream:String="";
for (var i:int=0;i < jpgStream.length ; i++)
{
hexStream += decToHex(jpgStream[i]);
}
function decToHex(dec:int):String
{
var hex:String = dec.toString(16).toUpperCase();
if (dec<16)
{
hex = "0"+hex;
}
return hex;
}
//transfer hexStream to the server
var variables:URLVariables = new URLVariables();
variables.hexStream = hexStream;
var myRequest:URLRequest = new URLRequest("saveimg.php");
myRequest.method = URLRequestMethod.POST;
myRequest.data = variables;
var loader:URLLoader = new URLLoader(myRequest);
loader.dataFormat = URLLoaderDataFormat.BINARY
loader.load(myRequest);
on the server side just one line of php code
file_put_contents('img.jpg', pack('H*', $_POST['hexStream']));
obviously this is slower than using HTTP_RAW_POST_DATA
but hey
精彩评论