C#의 진행중체로 웹에서 로컬 파일로 파일을 다운로드합니다.

StackOverflow https://stackoverflow.com/questions/1108292

  •  12-09-2019
  •  | 
  •  

문제

리모컨 (HTTP) 서버에서 사용자로 로컬 하드 드라이브로 일부 파일을 다운로드하는 작은 앱이 있습니다. 일부 파일은 크지 만 실행 시간에 얼마나 큰지는 모르겠습니다. 어떤 유형의 진행중체가있는 파일을 다운로드 할 수있는 메소드가 있습니까?

이것은 WinForms 앱입니다. 지금 당장 WebClient.DownloadFile ()을 사용하여 파일을 다운로드하고 있습니다.

편집 : DownloadProgressChanged 및 OndownloadProgressChanged 이벤트를 살펴 보았지만 제대로 작동하는 것처럼 보이지만 내 솔루션에는 효과가 없습니다. 여러 파일을 다운로드하고 있는데 WebClient.DownloadFileAsync를 사용하면 각 파일이 호출되므로 이벤트가 여러 번/초라고합니다.
다음은 앱의 기본 구조입니다.

  • 일반적으로 약 114의 파일 목록을 다운로드하십시오
  • 파일 목록을 통해 루프를 실행하고 각 파일을 탈의로 다운로드하십시오.

각 파일을 별개로 다운로드하는 것은 마음에 들지 않지만 downloadfileAsync ()로 다운로드하지 않고는 이벤트 처리기를 사용할 수 없습니다.

도움이 되었습니까?

해결책

사용 WebClient.ondownloadprogresschanged. 서버가 크기를 앞쪽으로보고하는 경우 진행 상황을 계산하는 것이 가능합니다.

편집하다:

업데이트를보고, 시도 할 수있는 것은 대기줄 URL의. 그런 다음 파일이 다운로드가 완료되면 (다운로드 dataCEMPleted 이벤트), 큐에서 다음 URL의 비동기 다운로드를 시작합니다. 나는 이것을 테스트하지 않았다.

다른 팁

웹 클라이언트를 처리하십시오 DownloadProgressChanged 이벤트.

나는 방금 이것을 썼고 그것은 확실히 당신이 원하는 것을하는 것처럼 보입니다.

또한 ProgressChanged 이벤트 내에서 "TotalByTestoreCeive"속성과 "바이 테스 레지 유효한"속성이 있습니다.

private void StartDownload()
{

    // Create a new WebClient instance.
    WebClient myWebClient = new WebClient();

    // Set the progress bar max to 100 for 100%
    progressBar1.Value = 0;
    progressBar1.Maximum = 100;

    // Assign the events to capture the progress percentage
    myWebClient.DownloadDataCompleted+=new DownloadDataCompletedEventHandler(myWebClient_DownloadDataCompleted);
    myWebClient.DownloadProgressChanged+=new DownloadProgressChangedEventHandler(myWebClient_DownloadProgressChanged);

    // Set the Uri to the file you wish to download and fire it off Async
    Uri uri = new Uri("http://external.ivirtualdocket.com/update.cab");
    myWebClient.DownloadFileAsync(uri, "C:\\Update.cab");

}

void myWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

void myWebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    progressBar1.Value = progressBar1.Maximum;
}

나는 비슷한 문제를 해결해야했고 GenericTypetea의 코드 예제는 속임수를했다. DownloadFileAsync 메소드를 호출 할 때 DownloadDatAcompleted 이벤트가 발사되지 않는다는 것을 제외하고. 대신, 다운로드 filecompleted 이벤트가 제기됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top