I want to upload a file using HttpClient to a php script in order to save it on the server in a Windows Phone 8.1 application.
Here is my C # code I received 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; } }
And with this code, I retrieve data in a PHP script
<? function base64_to_image( $imageData, $outputfile ) { $ifp = fopen( $outputfile, "wb" ); fwrite( $ifp, base64_decode( $imageData ) ); fclose( $ifp ); return( $outputfile ); } if (isset($_POST['image'])) { base64_to_jpeg($_POST['image'], "image.jpg"); } else die("no image data found"); ?>
But the result that I always get is "No data", although there is an image file. Am I doing something wrong by passing it as a POST parameter?
source share