Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 - POST form with an image

I need to send an image from the Windows Phone 7 to some e-mail addresses. I use this class to submit text values to a PHP script, wich parses data and sends a formatted e-mail to the addresses. The problem is that I can't figure out how to send an image to that script, to attach the image to the e-mail. The PHP script can be changed in any way. If I have an Image object, how can I change this class to allow sending images?

public class PostSubmitter
{
    public string url { get; set; }
    public Dictionary<string, string> parameters { get; set; }

    public PostSubmitter() { }

    public void Submit()
    {
        // Prepare web request...
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";

        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
    }

    private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);

        // Prepare Parameters String
        string parametersString = "";
        foreach (KeyValuePair<string, string> parameter in parameters)
        {
            parametersString = parametersString + (parametersString != "" ? "&" : "") + string.Format("{0}={1}", parameter.Key, parameter.Value);
        }

        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(parametersString);
        // Write to the request stream.
        postStream.Write(byteArray, 0, parametersString.Length);
        postStream.Close();
        // Start the asynchronous operation to get the response
        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        string responseString = streamRead.ReadToEnd();
        // Close the stream object
        streamResponse.Close();
        streamRead.Close();
        // Release the HttpWebResponse
        response.Close();
        //Action<string> act = new Action<string>(DisplayResponse);
        //this.Dispatcher.BeginInvoke(act, responseString);
    }

I use the class in this way:

Dictionary<string, string> data = new Dictionary<string, string>()
{
        {"nom", nom.Text},
        {"cognoms", cognoms.Text},
        {"email", email.Text},
        {"telefon", telefon.Text}
};

PostSubmitter post = new PostSubmitter() { url = "http://example.com/parserscript.php", parameters = data };
post.Submit();

Thank you very much!

like image 960
enkara Avatar asked Apr 11 '11 14:04

enkara


1 Answers

I've converted the above code to the following, I'm sure it will help:

public class PostSubmitter
{
    public string url { get; set; }
    public Dictionary<string, object> parameters { get; set; }
    string boundary = "----------" + DateTime.Now.Ticks.ToString();

    public PostSubmitter() { }

    public void Submit()
    {
        // Prepare web request...
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
        myRequest.Method = "POST";
        myRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);

        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
    }

    private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        Stream postStream = request.EndGetRequestStream(asynchronousResult);

        writeMultipartObject(postStream, parameters);
        postStream.Close();

        request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
        Stream streamResponse = response.GetResponseStream();
        StreamReader streamRead = new StreamReader(streamResponse);
        streamResponse.Close();
        streamRead.Close();
        // Release the HttpWebResponse
        response.Close();
    }


    public void writeMultipartObject(Stream stream, object data)
    {
        StreamWriter writer = new StreamWriter(stream);
        if (data != null)
        {
            foreach (var entry in data as Dictionary<string, object>)
            {
                WriteEntry(writer, entry.Key, entry.Value);
            }
        }
        writer.Write("--");
        writer.Write(boundary);
        writer.WriteLine("--");
        writer.Flush();
    }

    private void WriteEntry(StreamWriter writer, string key, object value)
    {
        if (value != null)
        {
            writer.Write("--");
            writer.WriteLine(boundary);
            if (value is byte[])
            {
                byte[] ba = value as byte[];

                writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""; filename=""{1}""", key, "sentPhoto.jpg");
                writer.WriteLine(@"Content-Type: application/octet-stream");
                //writer.WriteLine(@"Content-Type: image / jpeg");
                writer.WriteLine(@"Content-Length: " + ba.Length);
                writer.WriteLine();
                writer.Flush();
                Stream output = writer.BaseStream;

                output.Write(ba, 0, ba.Length);
                output.Flush();
                writer.WriteLine();
            }
            else
            {
                writer.WriteLine(@"Content-Disposition: form-data; name=""{0}""", key);
                writer.WriteLine();
                writer.WriteLine(value.ToString());
            }
        }
    }
}

To convert an image from the camera to an byte array I've used the follwing:

private void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            try
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
                foto.Source = image;

                using (MemoryStream ms = new MemoryStream())
                {
                    WriteableBitmap btmMap = new WriteableBitmap(image);

                    // write an image into the stream
                    Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, image.PixelHeight, 0, 100);

                    byteArray = ms.ToArray();
                }
            }
            catch (ArgumentNullException) { /* Nothing */ }
        }

And I use the class this way:

Dictionary<string, object> data = new Dictionary<string, object>()
        {
            {"nom", nom.Text},
            {"cognoms", cognoms.Text},
            {"email", email.Text},
            {"telefon", telefon.Text},
            {"comentari", comentari.Text},
            {"foto", byteArray},
        };
        PostSubmitter post = new PostSubmitter() { url = "http://example.com/parserscript.php", parameters = data};
        post.Submit();

I don't know if it's the best way to send an image from the phone to a server, but I couldn't find anything, so I made my own class just reading this and that, and it has taken me several days. If anybody wants to improve the code or write any comment will be welcomed.

like image 155
enkara Avatar answered Sep 20 '22 10:09

enkara