Windows phone app - uploading image to web server RRS feed

  • Question

  • So i used the below code to upload an image file the user selects to my webserver ,  but i get an error. Like the one below.


            private  void SmallUpload()
                    PhotoChooserTask photoChooserTask;
                    photoChooserTask = new PhotoChooserTask();
                    photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);

           private  void photoChooserTask_Completed(object sender, PhotoResult e)
                string serverUrl;
                Uri serverUri = null;
                NetworkCredential credential;
                Uri.TryCreate(ftpURIInfo +"/happy.jpg", UriKind.Absolute, out serverUri);
                serverUrl = serverUri.ToString();
                credential = new System.Net.NetworkCredential(us.Trim(),
                if (e.TaskResult == TaskResult.OK)
                     binReader = new BinaryReader(e.ChosenPhoto);
                     if (!(binReader.BaseStream.Length > 204800))
                        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(serverUri);
                         myRequest.Credentials = credential;
                        myRequest.Method = "STOR";
                        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);

     private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
                    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
                    System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);
                    byte[] byteArray = binReader.ReadBytes(204800).ToArray<byte>();//get bytes of choosen picture
                        // Write to the request stream.
                    postStream.Write(byteArray, 0, byteArray.Length);
                    //Start the asynchronous operation to get the response
                    request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
                catch (WebException e)

    When i run the code i get this error : 

    All the variables are not equal to null, they are set to a value in my class.

    • Edited by kalel111 Sunday, October 20, 2013 4:42 AM
    Sunday, October 20, 2013 4:40 AM

All replies

  • I would check the uri you are trying to create the http web request is correct.
    Sunday, October 20, 2013 3:07 PM
  • The Uri is fine. I use the same URI for my windows store app and i dont have a problem with it.

    Sunday, October 20, 2013 4:41 PM
  • This concerns me:




    You aren't checking If Uri.TryCreate works:

    Uri.TryCreate(ftpURIInfo +"/happy.jpg", UriKind.Absolute, out serverUri);
    = serverUri.ToString();

    If it returns false, serverURI is not valid because it couldn't be parsed.

    Darin R.

    Sunday, October 20, 2013 10:31 PM
  • I am using ftp because i am uploading the file to a webserver. For example. If the website is called www.Cookies.com , to upload to my websever, i would use the address " ftp://Cookies.com " 

    Keep in mind, this uri WORKS in my windows store app. I dont see why it doesnt work in windows phone. It's just a web address.

    Sunday, October 20, 2013 10:58 PM
  • Well, there's your problem.  HttpWebRequest doesn't do FTP.  You'll have to write a FTP client to upload using the FTP protocol.



    Darin R.

    Sunday, October 20, 2013 11:07 PM
  • So how exactly do i upload photos to a web server? I dont want to create a separate app for uploading files to my web server. This ability has to be part of my current app
    Sunday, October 20, 2013 11:10 PM
  • the STOR method isn't native to HTTP or FTP.  What kind of server are you uploading to?

    Are you sure it isn't some sort of FTP over HTTP proxy?  In your app, just change the URI to http:// or https:// and do the exact same thing you are doing. 

    On your server, you'll have to handle the POST request, and write the file bits from the Request's input stream.

    Just a question - why the 2MB file size check?  There is a Stream.CopyTo(Stream target) that will do the copy without putting the whole 2MB of data in a memory array before it's copied to the network memory stream before being sent out.

    Darin R.

    Sunday, October 20, 2013 11:15 PM
  • This is my new method for sending a picture file to my server.  i dont get errors, but i also dont see the picture on the server.

            HttpWebRequest r = (HttpWebRequest)WebRequest.Create(serverUri);
            r.Method = "POST";
            r.BeginGetRequestStream(delegate(IAsyncResult req)
                    var outStream = r.EndGetRequestStream(req);
                    r.BeginGetResponse(delegate(IAsyncResult result)
                                HttpWebResponse response = (HttpWebResponse)r.EndGetResponse(result);
                                using (var stream = response.GetResponseStream())
                                    using (StreamReader reader = new StreamReader(stream))
                                       String d = reader.ReadToEnd();
                            catch(Exception io)
                        }, null);
                }, null);

    Monday, October 21, 2013 11:16 AM
  • What is the code on the server doing?  It should read the input stream and write it to a file, database, or other storage.

    Darin R.

    Monday, October 21, 2013 4:05 PM
  • I dont have a code for the server. I used the same method for windows store apps and it worked fine. My images uploaded to the server. I didnt need to write any code for the server.
    Saturday, November 2, 2013 5:57 AM