Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight Http Post to upload images
    text
    copied!<p>I have been trying to perform an HTTP Post request to upload an image in a silverlight application for a windows phone 7 application. The sample codes online do not get me the desired response from the API. Could anyone please provide a working code which does this?</p> <p>By desired response I mean that the API responds saying that the uploaded file is in a format which cannot be read.</p> <p>Thanks in advance!</p> <p>Here is my code:</p> <pre><code> private void post_image(version, username,password,job-id, serviceUri) { if (session_free.bLoggedIn) { bool submit_success = false; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(serviceUri)); IsolatedStorageFileStream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile("file.jpg", FileMode.Open); request.PostMultiPartAsync(new Dictionary&lt;string, object&gt; { { "version", version }, { "username", user }, { "password", pass }, { filename, stream } }, new AsyncCallback(asyncResult =&gt; { Thread.Sleep(1000); HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); Post_Result = reader.ReadToEnd(); this.Dispatcher.BeginInvoke(delegate { MessageBox.Show(Post_Result); response.Close(); }); }), filename); Thread.Sleep(1000); } else { MessageBox.Show("User not signed in! Please login to continue...", "Invalid Authentication", MessageBoxButton.OK); } } public class DataContractMultiPartSerializer { private string boundary; public DataContractMultiPartSerializer(string boundary) { this.boundary = boundary; } private void WriteEntry(StreamWriter writer, string key, object value, string filename) { if (value != null) { writer.Write("--"); writer.WriteLine(boundary); if (value is IsolatedStorageFileStream) { IsolatedStorageFileStream f = value as IsolatedStorageFileStream; writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, filename); writer.WriteLine("Content-Type: image/jpeg"); writer.WriteLine("Content-Length: " + f.Length); writer.WriteLine(); writer.Flush(); Stream output = writer.BaseStream; Stream input = f; byte[] buffer = new byte[4096]; for (int size = input.Read(buffer, 0, buffer.Length); size &gt; 0; size = input.Read(buffer, 0, buffer.Length)) { output.Write(buffer, 0, size); } output.Flush(); writer.WriteLine(); } else { writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key); writer.WriteLine(); writer.WriteLine(value.ToString()); } } } public void WriteObject(Stream stream, object data, string filename) { StreamWriter writer = new StreamWriter(stream); if (data != null) { if (data is Dictionary&lt;string, object&gt;) { foreach (var entry in data as Dictionary&lt;string, object&gt;) { WriteEntry(writer, entry.Key, entry.Value, filename); } } else { foreach (var prop in data.GetType().GetFields()) { foreach (var attribute in prop.GetCustomAttributes(true)) { if (attribute is System.Runtime.Serialization.DataMemberAttribute) { DataMemberAttribute member = attribute as DataMemberAttribute; writer.Write("{0}={1}&amp;", member.Name ?? prop.Name, prop.GetValue(data)); } } } } writer.Flush(); } } } </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