как использовать этот метод postasync для отправки моего изображения в веб-сервис, я использую библиотеку system.net.http - PullRequest
0 голосов
/ 01 июня 2018
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
*using System.Net.Http;*
using Windows.Storage.Pickers;
using Windows.Storage;
using Windows.Graphics.Imaging;
using Windows.Media.Capture;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;

namespace Parrainage
{
    /// <summary>
    /// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private IRandomAccessStream stream;

        public MainPage()
        {
            this.InitializeComponent();
            grid2.Visibility = Visibility.Collapsed;
        }

        // Please look this method and help to add image to send
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            HttpClient client = new HttpClient();
            var JsonReponse = await client.GetStringAsync("http://localhost:50638/api/Filieres");
            var posts = JsonConvert.DeserializeObject<List<Filiere>>(JsonReponse);
            txtnivea.ItemsSource = posts;
            txtFilier.ItemsSource = posts;
        }
        private async void btnSuivant_Click(object sender, RoutedEventArgs e)
        {
             var etudiant = new Etudiant()
                 {
                  nom_etudiant = txtNom.Text,
                  prenom_etudiant = txtPrenom.Text
                  matricule = txtMatricule.Text,
                  mots_passe = txtMotsPasse.Text
                  /////picture = img.Source
                  }; 
             var etudiantJson = JsonConvert.SerializeObject(etudiant);
             var client = new HttpClient();
             var HttpContent = new StringContent(etudiantJson);
             HttpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
             await client.PostAsync("http://localhost:50638/api/Etudiants", HttpContent);
        }
}

1 Ответ

0 голосов
/ 04 июня 2018

Если вы хотите загрузить свой файл изображения в службу с помощью System.Net.Http API, вы можете попробовать следующий код:

public async Task<bool> UploadImage(StorageFile ImageFile, Uri uri)
{
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    var stream = await ImageFile.OpenAsync(FileAccessMode.Read);
    var fileContent = new System.Net.Http.StreamContent(stream.AsStream());
    fileContent.Headers.Add("Content-Type", "multipart/form-data");
    System.Net.Http.HttpResponseMessage response=await client.PostAsync(uri, fileContent);
    return response.IsSuccessStatusCode;
}
...