利用上传图片的HttpClient [英] Upload image using HttpClient
问题描述
我想用HttpClient的PHP脚本将其保存在一款Windows Phone 8.1应用程序的服务器上上传文件。
I want to upload a file using HttpClient to a php script to save it on a server in a Windows Phone 8.1 application.
下面是我的C#代码我从这个帖子。
Here is my C# code I got from this post.
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脚本数据
And with this code I retrieve the data in the php script
<?
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参数?
But the result I always get is "No Data found" although there IS an image file. Am I doing something wrong passing it as a POST parameter?
推荐答案
好吧研究几个小时后,我来到了这一点,我应该从草稿重新开始。
我模拟HTML表单上传与下面的C#代码:
Okay after hours of researching I came to the point that I should restart from draft. I simulate a Html form upload with following C# code:
private async Task<string> UploadImage(StorageFile file)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://your.url.com/");
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("fileToUpload");
form.Add(content, "fileToUpload");
var stream = await file.OpenStreamForReadAsync();
content = new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "fileToUpload",
FileName = file.Name
};
form.Add(content);
var response = await client.PostAsync("upload.php", form);
return response.Content.ReadAsStringAsync().Result;
}
和我的PHP文件接收数据看起来的follwing:
And my php file to receive the data looks as the follwing:
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
?>
现在它的作品,因为它应该,我希望有人可以重复使用我的代码。
Now it works as it should and I hope somebody can reuse my code.
这篇关于利用上传图片的HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!