Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:00:25+00:00 2026-06-08T21:00:25+00:00

I am working on a Windows Phone app. This app will let users take

  • 0

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.

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<byte>().ForEach(x => sb.AppendFormat("{0}.", Convert.ToUInt32(x)));

    Dictionary<string, string> parameters = new Dictionary<string, string>();
    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 += "&";
    }
    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<byte> fileBytes = new List<byte>();
  bytesToConvert.ToList<string>().Where(x => !string.IsNullOrEmpty(x)).ToList<string>().ForEach(x => 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();
  }
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:00:28+00:00Added an answer on June 8, 2026 at 9:00 pm

    You need to set ContentLength in your Upload method. To do that, you should compute your finished data buffer before you make the async request. Then you will have the length you need (after UTF8 encode). Then supply the data to the stream when the callback occurs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working windows phone 7 app which loads an image inside a HyperlinkButton. This
I am working on creating a Windows Phone app that will play a series
I'm trying to build JPG upload functionality into our Windows Phone app. The server-side
Hi I am working in windows 7 phone based app using silverlight. I have
I am working on a Windows Phone 7 app and I want to give
I'm working on an App on the windows phone which is very sound, heavy,
I am working on an app on Windows Phone and trying to get data
I'm working on a windows phone project where I have some xml files with
I'm in lean startup mode, working on a simple phone app that will be
In my windows phone 7 App I have a single line textbox. When the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.