Advertisement
nm9505

DownloadPdfFile Function With SystemNetHttp

Aug 24th, 2023 (edited)
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | Source Code | 0 0
  1. private async Task <string> DownloadPdfFileAsync()
  2.         {
  3.             var filePath = Path.Combine(FileSystem .AppDataDirectory, "CifradoEnRedes_122017.pdf");
  4.             if (File.Exists(filePath))
  5.             {
  6.                 return filePath;
  7.             }
  8.  
  9.             var httpClient = new HttpClient();
  10.             var pdfBytes = await httpClient.GetByteArrayAsync("https://1drv.ms/b/s!AtMYJuNNyNicilx0lIqs4Za0F9UA?e=T8uc1V");
  11.             try
  12.             {
  13.                 File.WriteAllBytes(filePath, pdfBytes);
  14.  
  15.                 return filePath;
  16.             }
  17.             catch (Exception ex)
  18.             {
  19.                 await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "Ok");
  20.             }
  21.  
  22.             return null;
  23.         }
  24. private async  void Btn2Click(object sender, EventArgs e)
  25.         {
  26.             App.MasterDet.IsPresented =false;
  27.             await App.MasterDet.Detail.Navigation.PushAsync(new DISPERSION());
  28.             var filePath = await DownloadPdfFileAsync();
  29.  
  30.             if (filePath != null)
  31.             {
  32.                 await Launcher.OpenAsync(new OpenFileRequest
  33.                 {
  34.                     File = new ReadOnlyFile(filePath)
  35.                 });
  36.             }
  37.            
  38.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement