【发布时间】:2015-02-10 01:35:27
【问题描述】:
我想使用 HttpClient 将文件上传到 php 脚本,以将其保存在 Windows Phone 8.1 应用程序的服务器上。
这是我从 this post 获得的 C# 代码。
private async Task<string> GetRawDataFromServer(byte[] data)
{
//Debug.WriteLine("byte[] data length:" + Convert.ToBase64String(data).Length);
var requestContent = new MultipartFormDataContent();
// here you can specify boundary if you need---^
var imageContent = new ByteArrayContent(data);
imageContent.Headers.ContentType =
MediaTypeHeaderValue.Parse("image/jpeg");
requestContent.Add(imageContent, "image", "image.jpg");
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://www.x.net/");
var result = client.PostAsync("test/fileupload.php", requestContent).Result;
return result.Content.ReadAsStringAsync().Result;
}
}
通过这段代码,我在 php 脚本中检索数据
<?
function base64_to_image( $imageData, $outputfile ) {
/* encode & write data (binary) */
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
if (isset($_POST['image'])) {
base64_to_jpeg($_POST['image'], "image.jpg");
}
else
die("no image data found");
?>
但我总是得到的结果是“找不到数据”,尽管有一个图像文件。将其作为 POST 参数传递是不是我做错了什么?
【问题讨论】:
-
一方面,不应该将
.Result用于异步方法,而应改为await(编译器应该警告您错误地使用了async)。您是否进行了网络跟踪以查看通过网络发送的内容? -
不,说实话我不知道该怎么做,我会查一下。
标签: c# windows-phone-8 windows-phone httpclient