1

I need to save a screenshot from Flash onto my local drive. Due to some constraints, I am unfortunately unable to use FileReference as it produces a dialogue box(any way to directly save locally?) or FileStream(AdobeAir) as I am restricted to only SWF.

So I have decided to try and use PHP to get the job done.

AS3

private function takePhoto():void {
  var bitmapData:BitmapData = new BitmapData(800, 650);
  var encoder:JPGEncoder = new JPGEncoder(100);
  var byteArray:ByteArray;
  var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");

  bitmapData.draw(camHolder);
  byteArray = encoder.encode(bitmapData);
  var myRequest:URLRequest = new URLRequest("save.php");
  myRequest.requestHeaders.push(header);
  myRequest.method = URLRequestMethod.POST;
  myRequest.data = byteArray;
  loader.addEventListener(Event.COMPLETE, imgSaved);
  loader.load(myRequest);
}

private function imgSaved(event:Event):void {
  trace(loader.data);
}

PHP

<?php
if(isset($GLOBALS["HTTP_RAW_POST_DATA"])){
  $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
  $filename = "test.jpg";
  file_put_contents($filename, $jpg);
}
?>

The strange thing about the trace from my AS3 imgSaved function is that it returns exactly word for word of my PHP code. No images were saved during this process as well. Any help would be appreciated!

user1234567
  • 333
  • 2
  • 10

1 Answers1

0

There's a tricky way of achieving this.

You'll need to provide a download link (a href HTML link) in your SWF for the user to click and begin downloading the image.

Follow the step below and see if it helps:

  1. Convert the Image ByteArray into a Base64 image using method1 or method2

  2. Create an anchor tag as shown below

    <a href="data:application/octet-stream;base64,xxxxx" download="filename.jpg">Download</a>

    where 'xxxxx' is the Base64 encoded string of the image.

  3. Now when user clicks on this link, the image should immediately get downloaded into the users designated 'Downloads' folder.

Disclaimer:

  1. This solution is inspired from another solution here.

  2. To the best of my knowledge this might work only when your application is opened in a browser and not when opened as a standalone or as an AIR app.

  3. I haven't tried this out myself so no guarantee of the solution being effective.

Community
  • 1
  • 1
Vikram Deshmukh
  • 12,304
  • 4
  • 36
  • 38
  • Thank you! Let me try this. – user1234567 Jan 09 '14 at 09:26
  • Hi srvikram13, if the screenshot were taken inside of flash and encoded using jpgencoder, how would I go about uploading it onto a webserver? – user1234567 Jan 09 '14 at 09:38
  • Long back I made an application that did something similar. I don't have that code now. But, you can try using the PHP code given in [**this**](http://www.beautifycode.com/webcam-flash-php-upload-to-server) tutorial to save the image on your webserver. – Vikram Deshmukh Jan 09 '14 at 09:44
  • @user3176502 when sending the data to the server base16 encode it first to remove all non-ascii characters. on the server side you have to base16 decode it and then write it to a file with the jpg extension. – The_asMan Jan 09 '14 at 21:18
  • I actually solved this by simply installing WAMP and saving using php on localhost. – user1234567 Jan 13 '14 at 10:30