2015-09-28 43 views
0

我正在開發一個應用程序在Windows Phone 8.1的C#,可以拍照併發送到一個服務器(在base64)與發佈請求。如何發送一個圖像base64到服務器的Windows Phone 8.1

所以我的問題是,我找不到一種方法,仍然適用於我的請求包含我的圖像base64在身體上,並將其發送到我的服務器。

private async void validPicture_Click(object sender, RoutedEventArgs e) 
    { 
     Encode("ms-appx:///xx/xxx.jpg"); 
     try 
     { 
      var client = new Windows.Web.Http.HttpClient(); 
      var uri = new Uri("http://x.x.x.x:x/picture/"); 
      string pathBase64 = Encode("ms-appx:///xx/xxx.jpg").ToString(); 

      Dictionary<string, string> pairs = new Dictionary<string, string>(); 
      pairs.Add("type", "recognition"); 

      HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(pairs); 
      Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, formContent); 

      string content = await response.Content.ReadAsStringAsync(); 
      if (response.IsSuccessStatusCode) 
      { 
      } 
     } 
     catch (Exception eu) 
     { 

     } 
    } 

如果你是一個問題或需要更多的信息,請告訴我。

謝謝你的時間。

回答

1

首先你必須從存儲器讀取圖像文件並將字節轉換爲base64字符串。

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(PHOTO_PATH)); 

     byte[] rawBytes; 
     using (Stream stream = await file.OpenStreamForReadAsync()) 
     { 
      rawBytes = new byte[stream.Length]; 
      await stream.ReadAsync(rawBytes, 0, rawBytes.Length); 
     } 

     string base64Content = Convert.ToBase64String(rawBytes); 

然後,你必須提出該內容的請求。我不確定你的服務器如何接受請求,但這裏是在內容中用該字符串發送請求的示例。

var httpClient = new Windows.Web.Http.HttpClient(); 
     IBuffer content = CryptographicBuffer.ConvertStringToBinary(base64Content, BinaryStringEncoding.Utf8); 

     var request = new HttpBufferContent(content); 

     HttpResponseMessage response = await httpClient.PostAsync(new Uri(SERVER_URL), request); 
相關問題