Note that there are some explanatory texts on larger screens.

plurals
  1. POEnsuring File Upload from Windows Phone to ASP.NET MVC 3
    primarykey
    data
    text
    <p>I am working on a Windows Phone app. This app will let users take pictures and upload them to my server. I have successfully implemented this. However, I've noticed that sometimes the picture does not get uploaded properly. I suspect this happens when someone puts the app to sleep before its done uploading. Or the connectivity gets interrupted. I'm really not sure how to do address this. I've included my code here. I'm hoping someone can provide some insight.</p> <pre><code>Client Side - Windows Phone App - Picture.cs -------------------------------------------- public event EventHandler Upload_Succeeded; public event EventHandler Upload_Failed; public void Upload() { try { WebRequest request = HttpWebRequest.Create(GetBackendPictureUploadUrl()); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.BeginGetRequestStream(new AsyncCallback(UploadBeginGetRequestStreamCallBack), request); } catch (Exception ex) { Upload_Failed(this, EventArgs.Empty); } } private void UploadBeginGetRequestStreamCallBack(IAsyncResult ar) { try { StringBuilder sb = new StringBuilder(); this.ImageBytes.ToList&lt;byte&gt;().ForEach(x =&gt; sb.AppendFormat("{0}.", Convert.ToUInt32(x))); Dictionary&lt;string, string&gt; parameters = new Dictionary&lt;string, string&gt;(); parameters.Add("username", GetUsername()); parameters.Add("pictureByts", sb.ToString()); string data = string.Empty; foreach (string key in parameters.Keys) { data += key; data += "="; data += parameters[key]; data += "&amp;"; } data = data.Substring(0, data.Length - 1); HttpWebRequest webRequest = (HttpWebRequest)(ar.AsyncState); using (Stream postStream = webRequest.EndGetRequestStream(ar)) { byte[] byteArray = Encoding.UTF8.GetBytes(data); postStream.Write(byteArray, 0, byteArray.Length); postStream.Close(); } webRequest.BeginGetResponse(new AsyncCallback(Upload_Completed), webRequest); } catch (Exception ex) { Upload_Failed(this, EventArgs.Empty); } } private void Upload_Completed(IAsyncResult result) { Upload_Succeeded(this, EventArgs.Empty); } Server Size - ASP.NET MVC - MyController.cs ------------------------------------------- [AcceptVerbs(HttpVerbs.Post)] public ActionResult AddPicture(string username, string pictureBytes) { string path = ConfigurationManager.AppSettings["pictureDirectory"] + "/" + username + "/"; if (Directory.Exists(path) == false) Directory.CreateDirectory(path); string filePath = path + "/" + Guid.NewGuid().ToString() + ".png"; // Save the picture to the file system string[] bytesToConvert = pictureBytes.Split('.'); List&lt;byte&gt; fileBytes = new List&lt;byte&gt;(); bytesToConvert.ToList&lt;string&gt;().Where(x =&gt; !string.IsNullOrEmpty(x)).ToList&lt;string&gt;().ForEach(x =&gt; fileBytes.Add(Convert.ToByte(x))); using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { byte[] bytes = fileBytes.ToArray(); fileStream.Write(bytes, 0, bytes.Length); fileStream.Close(); } } </code></pre> <p>I'm so confused. This code looks right in my opinion. But I've noticed that files are being written on my server. Sometimes they are just grey images. Sometimes it looks like part of the image was written but not the whole thing. What am I doing wrong? How do I ensure that the file is completely uploaded / written properly? There has to be some way of doing this. Clearly Facebook etc. are doing this. What am I doing wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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