I'm attempting so send over a byte[]
using HTTP Post to store into a MySQL database as an image but it's not encoding the byte[]
correctly or I'm simply doing something very wrong.
C# Code
public async Task<string> Post (byte[] image)
{
string data = data = string.Format ("METHOD=post&Image={0}", image);
HttpWebRequest r = await Info.createRequest (data);
using (var resp = await Task.Factory.FromAsync<WebResponse> (r.BeginGetResponse, r.EndGetResponse, null)) {
return "Finished";
}
}
/*Info.createRequest(data)*/
public static async Task<HttpWebRequest> createRequest (string json, string ContentType = "application/x-www-form-urlencoded", byte[] img = null)
{
(new CancellationTokenSource ()).Dispose ();
var request = WebRequest.Create (BaseUrl) as HttpWebRequest;
request.Method = "POST";
request.ContentType = ContentType;
byte[] postData = Encoding.UTF8.GetBytes (json);
using (var stream = await Task.Factory.FromAsync<Stream> (request.BeginGetRequestStream, request.EndGetRequestStream, request)) {
await stream.WriteAsync (postData, 0, postData.Length);
/*if (img != null)
await stream.WriteAsync (img, 0, img.Length);*/
//^ Didn't seme to work.
}
return request;
}
PHP Code
$null = NULL;
$stmt = $this->db->prepare("INSERT INTO Posts (Image) VALUES (?)");
$stmt->bind_param("b", $_POST["Image"]);
$stmt->execute();
$stmt->close();