1

My code wants to upload a picture to a server, as below, but it always fails. Do you know why?

   public static void SendRequest(System.Text.StringBuilder sReq, byte[] sbyteData, Action<UpLoadPicData, int> onEventResponse = null, Action onFinally = null)
    {
        WebClient wc = new WebClient();

        wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
        Uri u = new Uri(sReq.ToString());
        wc.Headers[HttpRequestHeader.ContentLength] = sReq.Length.ToString();
        wc.Headers[HttpRequestHeader.Accept] = "*/*";
        wc.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";

        wc.OpenWriteAsync(u, "POST", sbyteData);
    }

    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();

        }
    }
Kara
  • 6,115
  • 16
  • 50
  • 57
Eric
  • 21
  • 1
  • 4

2 Answers2

3
void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            image1.Source = bmp;
            byte[] sbytedata = ReadToEnd(e.ChosenPhoto);
            string s = sbytedata.ToString();
            WebClient wc = new WebClient();
            Uri u = new Uri("url here");
            wc.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
            wc.OpenWriteAsync(u, "POST", sbytedata);

        }
    }
    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(); ;

        }
    }
    public static byte[] ReadToEnd(System.IO.Stream stream)
    {
        long originalPosition = stream.Position;
        stream.Position = 0;

        try
        {
            byte[] readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            stream.Position = originalPosition;
        }
    }

I used your code... and it worked.. i have also attached the convert to byte[] from image source to this.. the image is taken from gallery

Confused_alot
  • 189
  • 2
  • 10
  • Although do note that there is no response to show if the image is uploaded.. while it is infact uploaded to the server in the background. – Confused_alot Nov 09 '11 at 09:52
1

Doing this with WebClient will be very tricky (I'm not sure if it's even possible) on the phone. Use HttpWebRequest instead.

Have a look at these other questions on the same subject:

Uploading an image using C# and WebRequest?
and
Upload files with HTTPWebrequest (multipart/form-data)

Community
  • 1
  • 1
Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • I have tried HttWebRequest, alwasy failure, with Error = {"The remote server returned an error: NotFound."} – Eric Mar 16 '11 at 09:01
  • I noticed there are "Referer" item in the http header, I don't why. – Eric Mar 16 '11 at 09:02