简体   繁体   中英

JavaScript to pass Multiple jpegs from Flash (AS3) to PHP

I have the following AS3 code which converts a bunch of BitMap screenshots to jepgs following a user request. (sourceArray stores the original bitmaps).

for (var i:int=0; i<sourceArray.length; i++)
    {
    var jpgEncoder:JPGEncoder = new JPGEncoder(10);
    var jpgStream:ByteArray = jpgEncoder.encode(sourceArray[i]);
    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");     
    var jpgURLRequest:String = new String("videoprocess.php?name=drawing" + i +".jpg");
    jpgURLRequest.requestHeaders.push(header);
    jpgURLRequest.method = URLRequestMethod.POST;
    jpgURLRequest.data = jpgStream;
    ExternalInterface.call("openWindow", jpgURLRequest );

My PHP code to save these jpegs is as follows:

 <?php
 if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] ) && isset ( $_GET['name'] ) ) {

//the image file name
$fileName = time();

// get the binary stream
$im = $GLOBALS["HTTP_RAW_POST_DATA"];

//write it
$fp = fopen($fileName, 'wb');
fwrite($fp, $im);
fclose($fp);
}
?>

Using the externalinterface.call call above, I'm trying to pass individual jpegs to the PHP file for saving on each turn of the loop, but so far I've been unable to pass the jpgURLRequest as an object. Would anyone be kind to enough to shed light on the JS code which would be needed to pass the jpeg from AS3 to PHP? Many thanks.

I dont see why you would want to use Javascript to pass the binary data from Flash to PHP. You ca do that from as3 using URLLoader and URLRequest and setting the method to "post". The code you should add looks like this:

var urlRequest = new URLRequest(jpgURLRequest);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = jpgStream;
urlRequest.requestHeaders = new Array(header);
var urlLoader = new URLLoader();
urlLoader.load(urlRequest);
//depending if you want to do something with the response you can add event listeners

Hope it helps.

UPDATE If you want to send the requests in order you should start by defining a global variable: var index:int=0 and define a function:

function loadSource() {
var jpgEncoder:JPGEncoder = new JPGEncoder(10);
var jpgStream:ByteArray = jpgEncoder.encode(sourceArray[index]);
//... rest of your code here  
var urlRequest = new URLRequest(jpgURLRequest);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = jpgStream;
urlRequest.requestHeaders = new Array(header);
var urlLoader = new URLLoader();
urlLoader.load(urlRequest);
urlLoader.addEventListener(Event.COMPLETE,completeEventListener);}

function completeEventListener(e:Event)
{
  index ++;
  if (index<sourceArray.length) loadSource();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM