Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to send Stream but able to send string to Restful service from Windows Phone 7?
    text
    copied!<p>I have been trying to send a image to restful service and some data with it. But i can send data (Name and Description of image) and also i created sql database to store data and data is added on it but i can't send image to the server.</p> <p>the code for service:</p> <pre><code> [WebInvoke(UriTemplate = "UploadPhoto/{fileName}/{description}", Method = "POST")] public void UploadPhoto(string fileName, string description, Stream fileContents) { byte[] buffer = new byte[32768]; MemoryStream ms = new MemoryStream(); int bytesRead, totalBytesRead = 0; do { bytesRead = fileContents.Read(buffer, 0, buffer.Length); totalBytesRead += bytesRead; ms.Write(buffer, 0, bytesRead); } while (bytesRead &gt; 0); // Save the photo on database. using (DataAcess data = new DataAcess()) { var photo = new Photo() { Name = fileName, Description = description, Data = ms.ToArray(), DateTime = DateTime.UtcNow, }; data.InsertPhoto(photo); } ms.Close(); Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead); } </code></pre> <p>And this is my code on client side. I am doing it on windows phone 7.</p> <pre><code> void btnNewPhoto_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("http://localhost:2557/photos"); string requestUrl = string.Format("{0}/UploadPhoto/{1}/{2}", uri, System.IO.Path.GetFileName(txtFileName.Text), txtDescription.Text); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl); request.Method = "POST"; request.BeginGetRequestStream (result =&gt; { // Sending the request. using (var requestStream = request.EndGetRequestStream(result)) { using (StreamWriter writer = new StreamWriter(requestStream)) { BinaryReader reader = new BinaryReader(requestStream); string s = imgPhoto.ToString(); byte[] byteArray = Encoding.UTF8.GetBytes(s.ToString()); requestStream.Write(byteArray, 0, byteArray.Length); requestStream.Close(); requestStream.Dispose(); //writer.Write(requestUrl); //writer.Flush(); } } // Getting the response. request.BeginGetResponse(responseResult =&gt; { var webResponse = request.EndGetResponse(responseResult); using (var responseStream = webResponse.GetResponseStream()) { using (var streamReader = new StreamReader(responseStream)) { string srresult = streamReader.ReadToEnd(); } } }, null); }, null); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload