0

I have a Binary stream that takes photo stream from the PhotoChooser Task in windows phone. I am trying to upload the picture that the user chooses onto my web server.

How do I copy the photo stream to the HttpRequest Stream?

So far I have something like this

BinaryStream BStream = new BinaryStream(StreamFromPhotoChooser);
HttpRequest request = new HttpRequest()

I know how to set the properties for the HttpRequest so that it uploads to the right place. My only problem is the ACTUAL uploading of the picture.

Touki
  • 7,465
  • 3
  • 41
  • 63
user1775297
  • 87
  • 11

1 Answers1

0

You can write or copy your binary stream to request stream by getting request stream and writing your stream to it. Here is some sort of code you may find useful for Web Request by HttpWebRequest and your problem to capy photo stream in httpRequest Stream is done in GetRequestStreamCallback()

private void UploadClick()
{
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("your URL of server");
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";

        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}

private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);


            //your binary stream to upload
            BinaryStream BStream = new BinaryStream(StreamFromPhotoChooser);
            //copy your data to request stream
            BStream.CopyTo(postStream);

            //OR

            byte[] byteArray = //get bytes of choosen picture
            // Write to the request stream.
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();

            //Start the asynchronous operation to get the response
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }
        catch (WebException e)
        {

        }
}

private void GetResponseCallback(IAsyncResult asynchronousResult)
{
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);

            //to read server responce 
            Stream streamResponse = response.GetResponseStream();
            string responseString = new StreamReader(streamResponse).ReadToEnd();
            // Close the stream object
            streamResponse.Close();

            // Release the HttpWebResponse
            response.Close();

            Dispatcher.BeginInvoke(() =>
                {
                    //Update UI here

                });
            }
        }
        catch (WebException e)
        {
            //Handle non success exception here                
        }                   
}
Abhishek
  • 995
  • 13
  • 28