Still trying to deal with the problem of sending images to the hosting. For the most part, I work at random. Here is what stuck in the application:

{ FileOpenPicker openPicker = new FileOpenPicker(); //Открываю изображение openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { if (file != null) { using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { //преобразую поток в байтовый массив byte[] fileBytes; using (var Stream = await file.OpenStreamForReadAsync()) { var binaryReader = new BinaryReader(Stream); fileBytes = binaryReader.ReadBytes((int)Stream.Length); } using (var client = new HttpClient()) { var apiUri = new Uri("Некий.Хостинг.php"); var imageBinaryContent = new ByteArrayContent(fileBytes); var multipartContent = new MultipartFormDataContent(); multipartContent.Add(imageBinaryContent, "image" ); var result = await client.PostAsync(apiUri, multipartContent); Name.Text = result.Content.ReadAsStringAsync().Result; } } } } else { Name.Text = "Operation cancelled."; } } 

Code php file on the server:

<?php $base=$_REQUEST['image']; $filename = $_REQUEST['filename']; // Decode Image $binary=base64_decode($base); header('Content-Type: bitmap; charset=utf-8'); $file = fopen("uploads/".$filename, 'wb'); // Create File fwrite($file, $binary); fclose($file); echo 'Image upload complete, Please check your php file directory'; ?>

But the file does not reach the destination ... Help please understand

    1 answer 1

    This question came to the rescue. It all worked. It remains to parse the code :-) https://stackoverflow.com/questions/27425043/upload-image-using-httpclient


    If not hard, check out my understanding of this code:

     HttpClient client = new HttpClient(); //создаем клиент который используется для приема и передачи данных client.BaseAddress = new Uri("http://your.url.com/"); //Адрес по которому будет искаться необходимый php файл MultipartFormDataContent form = new MultipartFormDataContent(); // Нечто вроде посылки которая умеет вмешать в себя несколько любых объектов HttpContent content = new StringContent("fileToUpload"); // Зачем то создается этот контент с таким именем... form.Add(content, "fileToUpload"); //мы запихивием этот "fileToUpload" в двух экземплярах? В виде переменной и просто текста? var stream = await file.OpenStreamForReadAsync(); //Создаем поток для передачи content = new StreamContent(stream); // "Включаем его" content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") // Создаем с какой-то целью заголовок в котором имя файла и опять "fileToUpload" { Name = "fileToUpload", FileName = file.Name }; form.Add(content); //Еще раз добавляем в посылку var response = await client.PostAsync("upload.php", form); //Отправляем return response.Content.ReadAsStringAsync().Result; //Ждем ответа