Frnak
Frnak

Reputation: 6802

WindowsPhone 8 - Upload image to php page

I already searched for a lot of different tutorials on how to upload an image from an windowsphonen 8 app to my php page (fileserver) - nothing works for me, so I 'm asking you.

This is my code to convert a Stream to Base64

    string PhotoStreamToBase64(Stream PhotoStream)
    {
        MemoryStream memoryStream = new MemoryStream();
        PhotoStream.CopyTo(memoryStream);
        byte[] result = memoryStream.ToArray();

        string base64img = System.Convert.ToBase64String(result);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < base64img.Length; i += 32766)
        {
            sb.Append(Uri.EscapeDataString(base64img.Substring(i, Math.Min(32766, base64img.Length - i))));
        }

        return sb.ToString();
    }

I catch the Image using photoChooserTask (this works fine, but I dont get a Stream I can use for the other Method)

    private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            MessageBox.Show(e.ChosenPhoto.Length.ToString());

            //Code to display the photo on the page in an image control named myImage.
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            MyImage.Source = bmp;
        }
    }

To Upload the image I tried this:

    public void UploadImageAsync(Stream PhotoStream)
    {
        try
        {
            WebClient w = new WebClient();
            w.Headers["Content-type"] = "application/x-www-form-urlencoded";

            string data = "id=1" +
                    "&_fake_status=200" +
                    "&type=base64" +
                    "&image=" + PhotoStreamToBase64(PhotoStream);

            w.UploadStringAsync(new Uri("http://myurl.de/php/app/changeimg.php", UriKind.Absolute), "POST", data);

        }
        catch (Exception ex)
        {
        }
    }

The last part is my php file

    function base64_to_image( $imageData, $outputfile ) {
    $ifp = fopen( $outputfile, "wb" );
    fwrite( $ifp, base64_decode( $imageData ) );
    fclose( $ifp );
    return( $outputfile );
    }       

    if (isset($_POST['image'])) {
    base64_to_jpeg($_POST['image'], "test".$_GET['id'].".jpg");
$file = 'people.txt';

     $person = "Win";

    file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
     }
    else
{
    die("no image data found");
$file = 'people.txt';

     $person = "Fail";

    file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
}

I got all these code snippets from different sources and somehow thought it's the best "idea" I could find. Does anyone maybe have an example code for my Problem? I 'm really new to Windowsphone dev and I need some serious help on that problem.

Upvotes: 0

Views: 699

Answers (1)

MishaU
MishaU

Reputation: 708

First off all, do not use WebClient class, use HttpClient and its method PostAsync and then it completely dependent on your Web API implementation

Upvotes: 1

Related Questions