1

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();
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
Destiny Faith
  • 257
  • 6
  • 21

2 Answers2

2

Using .NET 4.5 (you can use .NET 4.0 by adding the Microsoft.Net.Http package from NuGet) there is an easier way to simulate form requests.

Here is an example:

private System.IO.Stream Upload(string actionUrl, string paramString, Stream paramFileStream, byte [] paramFileBytes)
{
    HttpContent stringContent = new StringContent(paramString);
    HttpContent fileStreamContent = new StreamContent(paramFileStream);
    HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(stringContent, "param1", "param1");
        formData.Add(fileStreamContent, "file1", "file1");
        formData.Add(bytesContent, "file2", "file2");
        var response = client.PostAsync(actionUrl, formData).Result;
        if (!response.IsSuccessStatusCode)
        {
            return null;
        }
        return response.Content.ReadAsStreamAsync().Result;
    }
}
canolucas
  • 1,482
  • 1
  • 15
  • 32
2
string.Format("METHOD=post&Image={0}", image);

Will yield something like:

METHOD=post&Image=System.Byte[]

So nothing useful will be actually sent.

You can use Base64 encoding if you want to POST the binary data:

String base64 = System.Convert.ToBase64String(image)
string.Format("METHOD=post&Image={0}", base64 );

I am not sure how the reverse operation is done in PHP, but I think it is something using base64_decode:

$base64Image = base64_decode($_POST["Image"]);
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53