- Home /
wwwform send an array the right way!
Hello, i have been struggling with this part of my code for at least a week, but i can't find any information online for doing this,
the only thing i want to do is to send an array to a php script in my server, this can be done very easily in a lot of ways, i successfully done this with $_GET, (www), but my array is way to big, so i get an error. the other method is to post it via POST (wwwForm), but if i do it in .addField() it also gets an error because my array is to big.
Finally my last option is to send it via addBinaryData right? well i have a lot of headaches trying to do this because i ALLWAYS receive the error : 400 BAD REQUEST.
My code goes like this:
//I use a simple method to convert my multidimensional array to byte[]
public byte[] Encode (int[,] input)
{
int d0 = input.GetLength (0), d1 = input.GetLength (1);
byte[] raw = new byte[((d0 * d1) + 2) * 4];
Buffer.BlockCopy (BitConverter.GetBytes (d0), 0, raw, 0, 4);
Buffer.BlockCopy (BitConverter.GetBytes (d1), 0, raw, 4, 4);
int offset = 8;
for (int i0 = 0; i0 < d0; i0++)
for (int i1 = 0; i1 < d1; i1++) {
Buffer.BlockCopy (BitConverter.GetBytes (input [i0, i1]), 0,
raw, offset, 4);
offset += 4;
}
return raw;
}
//Here i convert my multidimensional array to byte[]
byte[] sendingByte = Encode(myArray);
//Create the WWWForm
WWWForm form = new WWWForm ();
//Add the byte[] for sending ( this is where i thing i made the errors, please help)
form.AddBinaryData ("file", sendingByte, "file.tmp", "application/octet-stream");
WWW w = new WWW (www.myserver.com/post.php, form);
yield return w;
Debug.Log (w.text);
if (w.error != null) {
Debug.LogError ( w.error);
} else {
Debug.Log ("Done");
}
}
Ok the thing is i only want to receive the array, convert it again to an array of int , so in my php script i can INSERT those values to a mysql database, is this the right way to do it? is there another option? Thank you !