2

There are quite a few questions on SO that deal with sending image ByteArrays from Actionscript to PHP, however, none seems to fit me specific problem.

I want to send ONE post request from my flash application that contains some text fiels as well as several images.

The sending of one image I got to work by sending one image's ByteArray directly as the URLRequest's .data, but when I want to add more images, this approach does not work any more. I figures out how to construct and post a multipart/form-data with text fields and also found how to mark up the image data so they will be available in PHP's $_FILES array.

BUT with this method, I can't get the image ByteArray data to send properly.

With this code that constructs the request in actionscript:

var request:URLRequest = new URLRequest(uri);
request.method = URLRequestMethod.POST;
request.contentType = "multipart/form-data; boundary=fooboundbar";

var _data:String ="";
_data += "--fooboundbar\n";
_data += "Content-Disposition: form-data; name=\"userId\"\n\n";

_data += _userId + "\n";
_data += "--fooboundbar\n";
_data += "Content-Disposition: form-data; name=\"title\"\n\n";

_data += _title + "\n";
_data += "--fooboundbar\n";
_data += "Content-Disposition: form-data; name=\"screenshot\"; filename=\"screenshot.jpg\"\n";
_data += "Content-Type: image/jpeg\n";
_data += "Content-Transfer-Encoding: binary\n\n";

_data += _screenshotByteArray.toString() + "\n";
_data += "--fooboundbar\n";
_data += "Content-Disposition: form-data; name=\"design\"; filename=\"design.jpg\"\n";
_data += "Content-Type: image/jpeg\n";
_data += "Content-Transfer-Encoding: binary\n\n";

_data += _designByteArray.toString() + "\n";
_data += "--fooboundbar\n";
_data += "Content-Disposition: form-data; name=\"test\"\n\n";

_data += "test\n";
_data += "--fooboundbar--";

request.data = _data;

trace(_data);

I get a proper trace from actionscript with the boundaries in place. The problem is, that the _screenshotByteArray and _designByteArray data contain line breaks which mess up the whole post. The request basically ends after the first line of the _screenshotByteArray and does not include the second image nor the last "test" form-data field.

The var_dump from my PHP script confirms this:

array(2) {
  ["userId"]=>
  string(1) "0"
  ["title"]=>
  string(9) "undefined"
}

array(1) {
  ["screenshot"]=>
  array(5) {
    ["name"]=>
    string(14) "screenshot.jpg"
    ["type"]=>
    string(0) ""
    ["tmp_name"]=>
    string(14) "/tmp/phpXZxIqg"
    ["error"]=>
    int(0)
    ["size"]=>
    int(8)
  }
}

Also note the ["size"]=>int(8); that is basically the first 8 bits, i.e. the first line of the ByteArray of the image.

I tried different Content-Types for the images, also tried a different Content-Transfer-Encoding, but that does not seem to take effect.

I also considered if I would just encode the ByteArrays with base64 and decode them on the PHP side, but that didn't work with the $_FILES array, nor could I get it to work by sending the base64 encoded image as a normal text field and then store it to a file, somehow the content-type got messed up with that.

SO, with all the questions about sending one encoded image from Actionscript to PHP, what is the correct way of sending multipe images and form data from Actionscript to PHP?

2
  • You've forgotten to put the --fooboundbar separator between on the "design" file, meaning its data is actually part of the "screenshot". Commented Feb 11, 2013 at 14:31
  • @MarcB Oh you are right! But that was a mere coping error. I double checked and in my actual code it is present; Also that does not explain why all of the screenshot file that arrives on the PHP is 8 bits. I'll update the code sample in the question. Commented Feb 11, 2013 at 14:57

1 Answer 1

2

This class is working for me for this case: https://github.com/jonasmonnier/Multipart.as

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.