locked
Upload an image with WebClient RRS feed

  • Question

  • Hey. I'm trying to upload an image with webclient but it's not working. Here is the code to upload an image to a server:
    //get the profile photo fand upload to server
                    using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream targetStream = isStore.OpenFile("profile_photo.jpg", FileMode.Open, FileAccess.Read))
                        {
                            BitmapImage img = new BitmapImage();
                            img.SetSource(targetStream);
                            byte[] data = ImageToBytes(img);
    
                            WebClient MyClient = new WebClient();
                            MyClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
                            MyClient.OpenWriteAsync(new Uri("http://www.iclips.co.za/UploadProfilePhoto.php"), "POST", data);
                        }
                    }

    public static void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    object[] objArr = e.UserState as object[];
                    byte[] fileContent = e.UserState as byte[];
    
                    Stream outputStream = e.Result;
                    outputStream.Write(fileContent, 0, fileContent.Length);
                    outputStream.Flush();
                    outputStream.Close();
                    string s = e.Result.ToString(); ;
                    MessageBox.Show(s);
                }
            }

    I get a message saying System.Net.WebClient+WebClientWriteStream. How do I achieve this? Thanks in advance
    Saturday, April 18, 2015 3:32 PM

Answers

  • Hi Clint,

    That means HttpClient works fine but WebClient does not work.

    I think you need to add header before trying to upload something.

    MyClient.Headers["Content-Type"] = "multipart/form-data";

    Try to use some network tracking tools to find out what's the difference between the message sent by HttpClient and WebClient, I think the biggest problem is header.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Jamles Hez Monday, April 27, 2015 12:54 PM
    • Proposed as answer by Jamles Hez Wednesday, May 6, 2015 10:55 AM
    • Marked as answer by Jamles Hez Thursday, May 7, 2015 1:29 AM
    Monday, April 27, 2015 12:46 PM

All replies

  • Hi Clint,

    What do you mean by I get a message saying System.Net.WebClient+WebClientWriteStream? I cannot understand this.

    I also want to know what's inside byte[] data = ImageToBytes(img); could it be return a incorrect byte array?

    Here I also found some good suggestions for you: http://stackoverflow.com/questions/19702956/windowsphone-8-upload-image-to-php-page

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 20, 2015 10:03 AM
  • I get that error message System.Net.WebClient+WebClientWriteStream in the messagebox in wc_OpenWriteCompleted. And this is in ImageToBytes(img):
    static byte[] ImageToBytes(BitmapImage img)
            {
                using (var ms = new MemoryStream())
                {
                    try
                    {
                        var btmMap = new WriteableBitmap(img);
                        btmMap.SaveJpeg(ms, img.PixelWidth, img.PixelHeight, 0, 100);
                        img = null;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("ImageToBytes Error:\n" + ex.Message);
                    }
                    return ms.ToArray();
                }
            }
    

    The link you gave says to use HttpClient and I did use that at first and it worked but I want to use WebClient to show the progress. According to the link it sounds like it's impossible to upload image with WebClient, is it? Or how do I do it using WebClient? Thanks in advance
    Wednesday, April 22, 2015 6:39 AM
  • Hi Clint,

    That means HttpClient works fine but WebClient does not work.

    I think you need to add header before trying to upload something.

    MyClient.Headers["Content-Type"] = "multipart/form-data";

    Try to use some network tracking tools to find out what's the difference between the message sent by HttpClient and WebClient, I think the biggest problem is header.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Jamles Hez Monday, April 27, 2015 12:54 PM
    • Proposed as answer by Jamles Hez Wednesday, May 6, 2015 10:55 AM
    • Marked as answer by Jamles Hez Thursday, May 7, 2015 1:29 AM
    Monday, April 27, 2015 12:46 PM