locked
[WP8.1][C#]Why when I try to upload an Image with Windows Phone it uploads a random thing?

    Question

  • I'm trying to upload an image from my Windows Store App (Windows Phone 8.1) to hosting (using PHP) but always when I "uploaded" the image, and I check its result the "image" it's a corrupted file. I think it's related to the byte array conversion but I haven't found another way to do it. This is my code:

    C# Code:

    byte[] ConvertBitmapToByteArray()
            {
                WriteableBitmap bmp = bitmap;
    
                using (Stream stream = bmp.PixelBuffer.AsStream())
                {
                    MemoryStream memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }
    
            public async Task<string> Upload()
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        using (var content =
                            new MultipartFormDataContent())
                        {
                            byte[] data = ConvertBitmapToByteArray();
                            content.Add(new StreamContent(new MemoryStream(data)), "userfile", fileNewImage);
    
                            using (
                               var message =
                                   await client.PostAsync("http://xplace.com/uploadtest.php", content))
                            {
                                var input = await message.Content.ReadAsStringAsync();
    
                                return input;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }

    PHP code:

    <?php
    	header('Content-Type: application/json');
    	$uploaddir = getcwd();
    	$uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);
    
    	if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    		echo '{"result": "sucessfull"}';
    	} else {
    	   echo '{"result": "-1"}';
    	}?>

    I wish someone could give me an idea about what I my mistake and how to fix it. Thanks in advance for your worthy knowledge.

    Federico Navarrete


    Wednesday, April 29, 2015 10:40 PM

Answers

  • This is the answer to the problem someone gave me an idea that I have missed to translate to BMP.

    byte[] ConvertBitmapToByteArray()
        {
            WriteableBitmap bmp = bitmap;
    
            using (Stream stream = bmp.PixelBuffer.AsStream())
            {
                MemoryStream memoryStream = new MemoryStream();
                stream.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }
    
        public async Task<string> Upload()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    using (var content =
                        new MultipartFormDataContent())
                    {
                        byte[] data = ConvertBitmapToByteArray();
    
    using (var stream = new InMemoryRandomAccessStream())
    {
        // encoder *outputs* to stream
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
    
        // encoder's input is the bitmap's pixel data
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, 
            (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, data);
    
        await encoder.FlushAsync();
    
        content.Add(new StreamContent(stream.AsStream()), "userfile", fileNewImage);
    
                        using (
                           var message =
                               await client.PostAsync("http://xplace.com/uploadtest.php", content))
                        {
                            var input = await message.Content.ReadAsStringAsync();
    
                            return input;
                        }
                    }
    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    Now I have a different problem that after I uploaded the Image to Azure, I cannot execute the PHP code this one:

    $uploaddir = getcwd();
    $uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        chmod($uploadfile, 0755);
        $Test = new Display($_FILES['userfile']['name']);
        echo '{"result": "' . $Test->getNumber($_REQUEST['color'], false) . '"}';
        //unlink($uploadfile);
    } else {
       echo '{"result": "-1"}';
    }

    I'd like to know what could be my bug because I don't understand why I can access from the URL, too to bit I cannot use it, maybe it's how I assigned the permissions but with or without the chmod, it doesn't change at all. I have even tried other hostings and the problem is the same when I enter to the File Manager, there are only my PHP files and it doesn't allow me to manage the image.

    Thanks for your worthy knowledge and experience.



    Federico Navarrete

    Thursday, April 30, 2015 12:32 PM

All replies

  • Hi FANMixco,

    I did not test your code, but I think it's better to do some self-check by trying the sample: https://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664#content

    You should be able to know how to upload things to the server by the HttpClient, the code you convert image to byte array looks fine.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 30, 2015 2:07 AM
    Moderator
  • Hi Federico,

    To help clarify your post and let people easily tell what it is about, please use a clear and concise subject and include applicable tags.

    We will help to modify the tag base on this sticky post, and thanks for your understanding.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 30, 2015 7:46 AM
    Moderator
  • This is the answer to the problem someone gave me an idea that I have missed to translate to BMP.

    byte[] ConvertBitmapToByteArray()
        {
            WriteableBitmap bmp = bitmap;
    
            using (Stream stream = bmp.PixelBuffer.AsStream())
            {
                MemoryStream memoryStream = new MemoryStream();
                stream.CopyTo(memoryStream);
                return memoryStream.ToArray();
            }
        }
    
        public async Task<string> Upload()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    using (var content =
                        new MultipartFormDataContent())
                    {
                        byte[] data = ConvertBitmapToByteArray();
    
    using (var stream = new InMemoryRandomAccessStream())
    {
        // encoder *outputs* to stream
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);
    
        // encoder's input is the bitmap's pixel data
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, 
            (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, data);
    
        await encoder.FlushAsync();
    
        content.Add(new StreamContent(stream.AsStream()), "userfile", fileNewImage);
    
                        using (
                           var message =
                               await client.PostAsync("http://xplace.com/uploadtest.php", content))
                        {
                            var input = await message.Content.ReadAsStringAsync();
    
                            return input;
                        }
                    }
    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    Now I have a different problem that after I uploaded the Image to Azure, I cannot execute the PHP code this one:

    $uploaddir = getcwd();
    $uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        chmod($uploadfile, 0755);
        $Test = new Display($_FILES['userfile']['name']);
        echo '{"result": "' . $Test->getNumber($_REQUEST['color'], false) . '"}';
        //unlink($uploadfile);
    } else {
       echo '{"result": "-1"}';
    }

    I'd like to know what could be my bug because I don't understand why I can access from the URL, too to bit I cannot use it, maybe it's how I assigned the permissions but with or without the chmod, it doesn't change at all. I have even tried other hostings and the problem is the same when I enter to the File Manager, there are only my PHP files and it doesn't allow me to manage the image.

    Thanks for your worthy knowledge and experience.



    Federico Navarrete

    Thursday, April 30, 2015 12:32 PM
  • Hi FANMixco,

    Nice to see your problem have been solved.

    Please do not ask multiple questions in one thread.

    Thanks for your understanding :)

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 1, 2015 2:42 AM
    Moderator