private async void Buy_Click(object sender, RoutedEventArgs e) { string sn = ""; LoadingBar.Visibility = Visibility.Visible; LoadingBar.IsIndeterminate = true; items2 = await Table2.Where(Book => Book.Id == rec.sel.Id).ToCollectionAsync(); BookData b = new BookData(); foreach (Book lol in items2) { b.Title = lol.Title; b.Author = lol.Author; b.userName = testlol; try { HttpClient client = new HttpClient(); // Create HttpClient imgBuffer = await client.GetByteArrayAsync(lol.ImageUri2); // Download file sn = JsonConvert.SerializeObject(b); } catch (Exception) { LoadingBar.Visibility = Visibility.Collapsed; await(new MessageDialog("Something bad happened :(:(")).ShowAsync(); break; } } try { var test = sender as Button; var test2 = test.Parent as Grid; var test3 = test2.Children[2] as TextBlock; var test4 = test2.Children[0] as TextBlock; string ab = test4.Text.Substring((test4.Text.Length) - 2); string nam = ""; foreach (char c in ab) { if (char.IsDigit(c)) { nam = nam + c.ToString(); } } string titl = Title.Text; Uri url = new Uri("https://www.ebookstreamer.me/downloads"); HttpClient httpClient = new HttpClient(); var myClientHandler = new HttpClientHandler(); //myClientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic; HttpResponseMessage httpResponse = new HttpResponseMessage(); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("id", test3.Text) }); httpResponse = await httpClient.PostAsync(url, content); httpResponse.EnsureSuccessStatusCode(); Stream str = await httpResponse.Content.ReadAsStreamAsync(); byte[] pd = new byte[str.Length]; str.Read(pd, 0, pd.Length); try { StorageFolder mainFol = await ApplicationData.Current.LocalFolder.CreateFolderAsync(testlol + "My Books", CreationCollisionOption.OpenIfExists); if (mainFol != null) { StorageFolder folder = await mainFol.CreateFolderAsync(titl, CreationCollisionOption.OpenIfExists); if (folder != null) { StorageFile file = await folder.CreateFileAsync(nam + ".txt", CreationCollisionOption.ReplaceExisting); using (var fileStream = await file.OpenStreamForWriteAsync()) { str.Seek(0, SeekOrigin.Begin); await str.CopyToAsync(fileStream); } StorageFile useFile = await folder.CreateFileAsync("UserName.txt", CreationCollisionOption.ReplaceExisting); await Windows.Storage.FileIO.WriteTextAsync(useFile, sn); StorageFile imgFile = await folder.CreateFileAsync("image.jpeg", CreationCollisionOption.ReplaceExisting); using (Stream stream = await imgFile.OpenStreamForWriteAsync()) stream.Write(imgBuffer, 0, imgBuffer.Length); // Save } } LoadingBar.Visibility = Visibility.Collapsed; await(new MessageDialog("Download Successful")).ShowAsync(); Frame.Navigate(typeof(Downloads)); } catch (Exception) { LoadingBar.Visibility = Visibility.Collapsed; await(new MessageDialog("Can't download now please try after sometime")).ShowAsync(); } } catch (Exception) { LoadingBar.Visibility = Visibility.Collapsed; await(new MessageDialog("Something bad happened :(:(")).ShowAsync(); } }
async Task retreive(string name) { try { List <GridClass> lg = new List <GridClass>(); GridClass gd = new GridClass(); if (name.CompareTo("about me") == 0) { gd.Image = new BitmapImage(new Uri(this.BaseUri, "Assets/whataboutme.jpg")); gd.authName = ""; gd.title = "about me"; lg.Add(gd); event1.Visibility = Visibility.Collapsed; event2.ItemsSource = lg; event2.Visibility = Visibility.Visible; } else { StorageFolder mainFol = await ApplicationData.Current.LocalFolder.CreateFolderAsync(testlol + "My Books", CreationCollisionOption.OpenIfExists); if (mainFol != null) { ob = new BookData(); StorageFolder folder = await mainFol.CreateFolderAsync(name, CreationCollisionOption.OpenIfExists); openBook = folder; StorageFile sampleFile = await folder.GetFileAsync("UserName.txt"); var t = await sampleFile.OpenAsync(FileAccessMode.Read); Stream na = t.AsStreamForRead(); using (var streamReader = new StreamReader(na, Encoding.UTF8)) { string line; line = streamReader.ReadToEnd(); ob = JsonConvert.DeserializeObject <BookData>(line); } IReadOnlyList <StorageFile> sf = await folder.GetFilesAsync(); StorageFile imgFile = await folder.GetFileAsync("image.jpeg"); Im = new BitmapImage(new Uri(imgFile.Path)); foreach (StorageFile s in sf) { gd = new GridClass(); if (s.Name.CompareTo("UserName.txt") == 0) { break; } if (s.Name.CompareTo("image.jpeg") == 0) { break; } gd = new GridClass(); gd.title = "Chapter No:" + s.DisplayName; gd.Image = Im; gd.authName = ""; lg.Add(gd); } event1.Visibility = Visibility.Collapsed; event2.ItemsSource = lg; event2.Visibility = Visibility.Visible; } } } catch (Exception) { await(new MessageDialog("ahhmm Something not so good Happened :(:(")).ShowAsync(); } }