1

I am trying to send a ByteArray to PHP which will then store it as a BLOB in my database. When I trace the ByteArray I get a length of 24,000 bytes which is good. When I send it to the database it appears something is not working right because when I return the database value I always get a length of 10. I made sure the table value is set at MEDIUM BLOB.

Here is my code in AS3:

var jpg:JPGEncoder = new JPGEncoder(70);
var byteArray:ByteArray = jpg.encode(cropData);
var phpVars:URLVariables = new URLVariables();

var phpFileRequest:URLRequest = new URLRequest("http://");

        phpFileRequest.method = URLRequestMethod.POST;

        phpFileRequest.data = phpVars;

        var phpLoader:URLLoader = new URLLoader();
        phpLoader.dataFormat = URLLoaderDataFormat.BINARY;     
        phpLoader.addEventListener(Event.COMPLETE, confirmUpload);

        //phpVars.systemCall = "pullProfile";
        phpVars.picArray = byteArray;
        phpLoader.load(phpFileRequest);

Here is my code in PHP:

    <?php
include_once("connect.php");
$loggeduserapp = $_POST['loggeduserapp'];
$picArray = $_POST['picArray'];
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "UPDATE athletes.userinfo SET profilepicture='$picArray' WHERE email='$loggeduserapp'";
$conn->exec($sql)

?>

Perhaps I am loading the BLOB information the wrong way? Right now, I have it in a JSON Object because I am reading other information as well for the user and I am referencing it like this.

    var pbyteArray:ByteArray = new ByteArray();
    trace(profileArray.profilepicture); //profileArray is my JSON Object
    pbyteArray.writeObject(profileArray.profilepicture);
    trace(pbyteArray.length);

I'm not sure what to do at this point, any help is much appreciated. Thanks!

Troy Myers
  • 165
  • 8
  • 1
    You don't send raw binary data like this. **URLVariables** works with plain data only: **String**, **int**, **Number**, **Boolean**. Learn how to send binary data with AS3 and receive them with PHP here: https://stackoverflow.com/questions/46465767/how-to-save-movieclip-as-png-image-to-mysql-database – Organis Oct 15 '17 at 19:04
  • I don't think `bytes.writeObject(x)` will give you an image. Show your JSON text for `profilepicture` entry. Is it hex like `FFFDFFE0...etc`? or is it a URL link to the jpeg on server? – VC.One Oct 15 '17 at 19:14
  • I just checked the BLOB entry within the database itself and it looks like I am just having a problem entering the ByteArray information. It shows ÿÃÿàwith a size of 8 bytes. I will look into the link Organis sent. I am trying to send the ByteArray itself to be placed into the database rather than a URL Link. – Troy Myers Oct 15 '17 at 19:16
  • That asker's code in @Organis' link is doing what you want. Try the same technique and then [**update**](https://stackoverflow.com/posts/46758471/edit) your question with new details if any issues. – VC.One Oct 15 '17 at 19:35
  • @TroyMyers You need to assign your whole **ButeArray** to **URLRequest.data** field. The mentioned thread is about sending image binary data, but it is actually irrelevant. Though you can look for another example with **Google > as3 send bytearray post data**, there are plenty of relevant results. – Organis Oct 15 '17 at 20:33
  • What I ended up doing was encoding the Byte Array with Base64 then sending it to my database and decode it on return and it works great. Thanks for the help! – Troy Myers Oct 15 '17 at 21:44
  • 1
    Troy, you can post that as an answer to your question instead of leaving it as a comment. – Brian Oct 17 '17 at 23:15

0 Answers0