在使用 PhotoChooserTask 选择后,我试图将图像上传到我的 php 服务器并写入服务器中的文件。我使用 UTF8 进行编码。但是在服务器中,无论原始图像大小如何,我都得到一个只有 1 KB 的文件。然后我将其编码为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";
}
?>