服务器损坏使用Windows Phone上传的图像

时间:2013-10-01 10:38:07

标签: windows-phone-7 windows-phone-8 windows-phone

我尝试使用PhotoChooserTask选择上传到我的php服务器并在服务器中写入文件时上传图像。我使用UTF8进行编码。但是在服务器中,我得到一个只有1千字节的文件,无论原始图像的大小是多少然后我将它编码为base64string并在服务器中解码。现在我得到的文件大小为4/3 * imagesize(没有解码),但在解码后我无法获得任何图像(文件大小等于原始文件大小)。我尝试过多种方式(用于阅读图像)但未能解决这个问题。可能是什么问题?或者我可以提出其他方法吗?

客户代码:

  PhotoChooserTask selectphoto = new PhotoChooserTask();
         selectphoto.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
        selectphoto.Show();




     void photoChooserTask_Completed(object sender, PhotoResult e)
                {
                    if (e.TaskResult == TaskResult.OK)
                    {
        System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
                        bmp.SetSource(e.ChosenPhoto);
                      byte[] data = null;
                    using (MemoryStream stream = new MemoryStream())
                        {
                            WriteableBitmap wBitmap = new WriteableBitmap(bmp);
                            wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
                            stream.Seek(0, SeekOrigin.Begin);
                            data = stream.GetBuffer();

                        }
         string utfData = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.1.49/xampp/imageserver.php");
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    postData = String.Format("image={0}", utfData);   

                    // Getting the request stream.
                    request.BeginGetRequestStream
                        (result =>
                        {
                            // Sending the request.
                            using (var requestStream = request.EndGetRequestStream(result))
                            {
                                using (StreamWriter writer = new StreamWriter(requestStream))
                                {
                                  writer.Write(postData);

                                    writer.Flush();
                                }
                            }

                            // Getting the response.
                            request.BeginGetResponse(responseResult =>
                            {
                                var webResponse = request.EndGetResponse(responseResult);
                                using (var responseStream = webResponse.GetResponseStream())
                                {
                                    using (var streamReader = new StreamReader(responseStream))
                                    {
                                        string srresult = streamReader.ReadToEnd();
                                        System.Diagnostics.Debug.WriteLine("sssssrreeeeeessssulllltttttt========="+srresult);
                                    }
                                }
                            }, null);
                        }, null);
    }

在服务器中:

<?php
if (isset($_POST['image'])) {
 $ifp = fopen( "withoutdecode.txt", "wb" );
    fwrite( $ifp, (($_POST['image'])) );
    fclose( $ifp );
$ifp2 = fopen( "theImage.png", "wb" );
    fwrite( $ifp2, utf8_decode(($_POST['image'])) );
    fclose( $ifp2 );
}
else
{
    die("no image data found");
echo "fail";
}
?>

2 个答案:

答案 0 :(得分:1)

试试这个:

WP侧的

(使用图像重新算法)

//将图片转换为字符串

public string ImageToByte(BitmapImage imageSource)         {             MemoryStream ms = new MemoryStream();             WriteableBitmap wb = new WriteableBitmap(imageSource);             wb.SaveJpeg(ms,imageSource.PixelWidth,imageSource.PixelHeight,0,100);             byte [] imageBytes = ms.ToArray();

        string result = Convert.ToBase64String(imageBytes);
        return result;
    }

//照片选择器任务已完成

BitmapImage image = new BitmapImage();                     image.SetSource(e.ChosenPhoto);

                string fileName = DateTime.Now.Ticks.ToString() + ".jpg";

                State["filename"] = fileName;

                // Load the picture back into our image
                BitmapImage b = new BitmapImage();
                b.CreateOptions = BitmapCreateOptions.None;
                b.SetSource(e.ChosenPhoto);

                double actualHeight = b.PixelHeight;
                double actualWidth = b.PixelWidth;
                double maxHeight = 600;
                double maxWidth = 800;
                double imgRatio = actualWidth / actualHeight;
                double maxRatio = maxWidth / maxHeight;
                double compressionQuality = 0.5;

                if (actualHeight > maxHeight || actualWidth > maxWidth)
                {
                    if (imgRatio < maxRatio)
                    {
                        //adjust width according to maxHeight
                        imgRatio = maxHeight / actualHeight;
                        actualWidth = imgRatio * actualWidth;
                        actualHeight = maxHeight;
                    }
                    else if (imgRatio > maxRatio)
                    {
                        //adjust height according to maxWidth
                        imgRatio = maxWidth / actualWidth;
                        actualHeight = imgRatio * actualHeight;
                        actualWidth = maxWidth;
                    }
                    else
                    {
                        actualHeight = maxHeight;
                        actualWidth = maxWidth;
                    }
                }

                int newh = Convert.ToInt32(ActualHeight);
                int neww = Convert.ToInt32(actualWidth);

                WriteableBitmap wb = new WriteableBitmap(b);
                WriteableBitmap wb1 = wb.Resize(neww, newh, WriteableBitmapExtensions.Interpolation.Bilinear);

                // Save the image into isolated storage
                using (IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream targetStream = isStore.OpenFile(fileName, FileMode.Create, FileAccess.Write))
                    {
                        WriteableBitmap bitmap = new WriteableBitmap(wb1);
                        bitmap.SaveJpeg(targetStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 70);
                        byte[] content = new byte[e.ChosenPhoto.Length];
                        e.ChosenPhoto.Read(content, 0, content.Length);
                        targetStream.Write(content, 0, content.Length);
                        targetStream.Flush();
                    }
                }

                BitmapImage fbmp = new BitmapImage();
                using (MemoryStream ms = new MemoryStream())
                {
                    wb1.SaveJpeg(ms, neww, newh, 0, 100);
                    fbmp.SetSource(ms);
                }

                string img64 = ImageToByte(fbmp);

将img64发送到服务器,就像在做什么一样!它应该工作

答案 1 :(得分:0)

我终于得到了解决方案。

问题是 - 当我使用Base64进行编码和解码时,网络上的一些不安全字符也被编码。它们是'+','/'和'='。在服务器中找不到这些字符。所以,我将'+','/'替换为' - ','_'并删除了'='。在服务器中,完成了相反的工作(对已编辑的base64进行了解析)字符串回到原始base64字符串)。现在我在解码后得到了服务器中的图像。