// // Used for demo only. // public async Task <string> UploadImageDemoAsync(PictureCreation picCreation, Stream fileStream) { string url = $"{ HttpRepositorySettings.BaseApiUrl }/{ "UWPPicturesDemo" }"; // Create the content var content = new MultipartFormDataContent(); content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data"); var jsonData = JsonSerializer.Serialize <PictureCreation>(picCreation, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); content.Add(new StringContent(jsonData, Encoding.UTF8, "application/json")); content.Add(new StreamContent(fileStream, (int)fileStream.Length), "image", picCreation.PictureFileName); var response = await _client.PostAsync(url, content); if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsStringAsync()); } return(null); }
private async Task UploadPicAsync() { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); picker.FileTypeFilter.Add(".png"); picker.FileTypeFilter.Add(".gif"); StorageFile file = await picker.PickSingleFileAsync(); if (file == null) { return; } using (var client = new HttpClient()) using (var fileStream = await file.OpenReadAsync()) { var pictureCreation = new PictureCreation { PictureFileName = file.Name, Caption = string.Empty, SetAsProfilePic = true, OrphanID = MasterMenuItem.OrphanID }; var picRepository = new PictureHttpRepository(client); if (AppSettings.UseWebApi) { MasterMenuItem.ProfilePicUri = await picRepository.UploadImageAsync(pictureCreation, fileStream.AsStreamForRead()); // // TODO re-render the view // Uri uri = new Uri(MasterMenuItem.ProfilePicUri); BitmapImage img = new BitmapImage(uri); imgProfilePic.Source = img; } else // Direct to db { MasterMenuItem.ProfilePicUri = await picRepository.UploadImageDemoAsync(pictureCreation, fileStream.AsStreamForRead()); // // TODO re-render the view // Uri uri = new Uri(MasterMenuItem.ProfilePicUri); BitmapImage img = new BitmapImage(uri); imgProfilePic.Source = img; } } }