1

I have the code that have to upload image to the uri with parametr file1. But the code doesn't work. Why an image doesn't uploading?

Here is my code:

public void Upload
{
    string oauthUrl = "http://MY_Uri";
    HttpClient theAuthClient = new HttpClient();

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, oauthUrl);
    StorageFolder storageFolder = KnownFolders.PicturesLibrary;
    StorageFile sf = await storageFolder.GetFileAsync("ss.png");
    IBuffer buffer = await FileIO.ReadBufferAsync(sf);
    byte[] fileData = buffer.ToArray();
    Encoding encoding = Encoding.GetEncoding("Windows-1252");
    string text = encoding.GetString(fileData, 0, fileData.Length);
    string content = @"file1=" + text + "";

    txt.Text = content;
    StorageFolder storageFolder2 = KnownFolders.PicturesLibrary;
    StorageFile sampleFile = await storageFolder2.CreateFileAsync("sample.txt");
    await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "" + text + "");

    request.Method = HttpMethod.Post;
    request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));
    request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

    try
    {
        HttpResponseMessage response = await theAuthClient.SendAsync(request);
        handleResponse(response);
    }
    catch (HttpRequestException hre)
    {

    }
}
public async void handleResponse(HttpResponseMessage response)
{
    string content = await response.Content.ReadAsStringAsync();
    Account account = JsonConvert.DeserializeObject<Account>(content);
    if (content != null)
    {

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
dkilov fuss
  • 97
  • 10

2 Answers2

1

Answered a similar question recently. File content is sent in the Body of the request.

  1. Remove your line : "request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));"
  2. Change your header for Content-Type to "image/png" or "application/octet-stream".
  3. Write your byte array to the request this way (imb being your byte[] for your image) :

    using (Stream os = request.GetRequestStream()) { os.Write(imb, 0, imb.Length); }

A link to a similar question : A previous answer

Community
  • 1
  • 1
Slack Shot
  • 1,090
  • 6
  • 10
  • I have similar code in Xamarin (Mono), Written similar code in Java. This is about HTTP Protocol, and not about platform. – Slack Shot Sep 11 '13 at 16:35
  • When you form encode your image.. for your post, if you see the request come in.. you'll notice his comment, about how to "reconstruct" the image.. If you provide the wrong information to the server about what the content is, it won't have any way to handle what you are giving it. – Slack Shot Sep 11 '13 at 16:36
  • But request.GetRequestStream() doesn't work with windows 8 api – dkilov fuss Sep 11 '13 at 16:36
  • http://stackoverflow.com/questions/14344029/system-net-httpwebrequest-does-not-contain-a-definition-for-getrequeststream – Slack Shot Sep 11 '13 at 16:38
0

This is the code I am using to upload an image to my PHP server in my Windows 8 app

            string serverUrl = "http://www.mywebsite.com/receiveImage.php";

            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(serverUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            try
            {
                IBuffer buffer = await FileIO.ReadBufferAsync(file);
                byte[] fileData = System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(buffer.ToArray()).Replace("+","%2B"));
                byte[] prefix = System.Text.Encoding.UTF8.GetBytes("ImageData=");
                byte[] combinedData = new byte[fileData.Length + prefix.Length];
                System.Buffer.BlockCopy(prefix, 0, combinedData, 0, prefix.Length);
                System.Buffer.BlockCopy(fileData, 0, combinedData, prefix.Length, fileData.Length);

                Stream requestStream = await webRequest.GetRequestStreamAsync();
                requestStream.Write(combinedData, 0, combinedData.Length);


                // Get the response from the server.
                WebResponse response = await webRequest.GetResponseAsync();
                StreamReader requestReader = new StreamReader(response.GetResponseStream());
                String webResponse = requestReader.ReadToEnd();
            }
            catch (Exception ex)
            {

            }

The Replace("+","%2B") is important. Without that + characters are converted to spaces when PHP receives them.

Real World
  • 1,593
  • 1
  • 21
  • 46