■ WebClient 클래스의 OpenReadAsync 메소드를 사용하는 방법을 보여준다.
▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<Grid x:Name="grid"> <Grid.RowDefinitions> <RowDefinition Height="100" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal"> <TextBlock x:Name="downloadPercentTextBlock" FontSize="20" FontWeight="Bold" Text="다운로드 퍼센트 : " /> <TextBlock x:Name="downloadPercentValueTextBlock" FontSize="20" FontWeight="Bold" Text="0" /> <TextBlock FontSize="20" FontWeight="Bold" Text="%" /> </StackPanel> <StackPanel Grid.Row="1" HorizontalAlignment="Center" Orientation="Horizontal"> <Button x:Name="downloadButton" Grid.Row="1" Width="100" Height="30" Content="다운로드" /> <Button x:Name="cancelButton" Grid.Row="1" Margin="10 0 0 0 " Width="100" Height="30" Content="다운로드 취소" /> </StackPanel> </Grid> |
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
using System; using System.IO; using System.IO.IsolatedStorage; using System.Net; using System.Windows; ... /// <summary> /// 웹 클라이언트 /// </summary> private WebClient webClient = null; ... downloadButton.Click += downloadButton_Click; cancelButton.Click += cancelButton_Click; ... #region 다운로드 버튼 클릭시 처리하기 - downloadButton_Click(sender, e) /// <summary> /// 다운로드 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void downloadButton_Click(object sender, RoutedEventArgs e) { if(this.webClient == null) { this.webClient = new WebClient(); } this.webClient.DownloadProgressChanged += webClient_DownloadProgressChanged; this.webClient.OpenReadCompleted += webClient_OpenReadCompleted; this.webClient.OpenReadAsync(new Uri("http://127.0.0.1/sample.txt", UriKind.Absolute)); } #endregion #region 다운로드 취소 버튼 클릭시 처리하기 - cancelButton_Click(sender, e) /// <summary> /// 다운로드 취소 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cancelButton_Click(object sender, RoutedEventArgs e) { if(this.webClient != null) { if(this.webClient.IsBusy) { this.webClient.CancelAsync(); } } } #endregion #region 웹 클라이언트 다운로드 진행 상태 변경시 처리하기 - webClient_DownloadProgressChanged(sender, e) /// <summary> /// 웹 클라이언트 다운로드 진행 상태 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { this.downloadPercentValueTextBlock.Text = e.ProgressPercentage.ToString(); } #endregion #region 웹 클라이언트 읽기 완료시 처리하기 - webClient_OpenReadCompleted(sender, e) /// <summary> /// 웹 클라이언트 읽기 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if(e.Cancelled) { MessageBox.Show("작업이 취소되었습니다."); return; } if(e.Error != null) { MessageBox.Show(e.Error.ToString()); return; } byte[] bufferByteArray = new byte[1024]; IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.OpenFile("sample.txt", FileMode.Create); while(true) { int byteCount = e.Result.Read(bufferByteArray, 0, 1024); if(byteCount == 0) { break; } isolatedStorageFileStream.Write(bufferByteArray, 0, byteCount); } isolatedStorageFileStream.Close(); } #endregion |