From 0acfaecb5998a2e78d4ccd29976a91994488139b Mon Sep 17 00:00:00 2001 From: jancoow Date: Tue, 19 Jan 2016 23:05:12 +0100 Subject: [PATCH] Added fileupload --- YJMPD-UWP/Model/NetworkHandler.cs | 6 ++++- YJMPD-UWP/Model/PhotoHandler.cs | 40 +++++++++++++++++++++++++++++++ YJMPD-UWP/YJMPD-UWP.csproj | 4 +++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/YJMPD-UWP/Model/NetworkHandler.cs b/YJMPD-UWP/Model/NetworkHandler.cs index 74d1f78..6ba5c3f 100644 --- a/YJMPD-UWP/Model/NetworkHandler.cs +++ b/YJMPD-UWP/Model/NetworkHandler.cs @@ -10,6 +10,9 @@ using Newtonsoft.Json; using System.Diagnostics; using System.IO; using Windows.Foundation; +using System.Net.Http; +using System.Text; +using Windows.Storage; namespace YJMPD_UWP.Model { @@ -82,7 +85,7 @@ namespace YJMPD_UWP.Model //din.UnicodeEncoding = UnicodeEncoding.Utf8; //din.ByteOrder = ByteOrder.LittleEndian; - dout.UnicodeEncoding = UnicodeEncoding.Utf8; + dout.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; dout.ByteOrder = ByteOrder.LittleEndian; UpdateNetworkStatus(NetworkStatus.CONNECTED); @@ -150,5 +153,6 @@ namespace YJMPD_UWP.Model App.Api.HandleMessage(o); } + } } diff --git a/YJMPD-UWP/Model/PhotoHandler.cs b/YJMPD-UWP/Model/PhotoHandler.cs index 8dccad1..ce87dc0 100644 --- a/YJMPD-UWP/Model/PhotoHandler.cs +++ b/YJMPD-UWP/Model/PhotoHandler.cs @@ -1,4 +1,7 @@ using System; +using System.Diagnostics; +using System.IO; +using System.Net.Http; using System.Threading.Tasks; using Windows.Graphics.Imaging; using Windows.Media.Capture; @@ -60,7 +63,44 @@ namespace YJMPD_UWP.Model SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(softwareBitmapBGR8); + await UploadImage(stream.AsStream()); + UpdatePhotoTaken(bitmapSource); } + + public async Task UploadImage(Stream file) + { + string url = "http://jancokock.me/f/?plain"; + MultipartFormDataContent postdata = new MultipartFormDataContent(); + postdata.Add(new ByteArrayContent(ReadFully(file)), "file", "capture.png"); + using (HttpClient hc = new HttpClient()) + { + try + { + var response = await hc.PostAsync(url, postdata); + response.EnsureSuccessStatusCode(); + Debug.WriteLine(await response.Content.ReadAsStringAsync()); + return await response.Content.ReadAsStringAsync(); + } + catch + { + throw new IOException("Network error"); + } + } + } + + public byte[] ReadFully(Stream input) + { + byte[] buffer = new byte[16 * 1024]; + using (MemoryStream ms = new MemoryStream()) + { + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + { + ms.Write(buffer, 0, read); + } + return ms.ToArray(); + } + } } } diff --git a/YJMPD-UWP/YJMPD-UWP.csproj b/YJMPD-UWP/YJMPD-UWP.csproj index 5364262..81c32ea 100644 --- a/YJMPD-UWP/YJMPD-UWP.csproj +++ b/YJMPD-UWP/YJMPD-UWP.csproj @@ -168,7 +168,9 @@ - + + PreserveNewest +