2015-02-06 46 views
-1

我有這樣的方法:如何計算文件下載百分比的進度?

HttpWebRequest request; 
     void fileDownloadRadar(string uri, string fileName) 
     { 
      request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri); 
      request.CookieContainer = new CookieContainer(); 
      request.AllowAutoRedirect = true; 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      if (response.ContentType == "") 
      { 
       Logger.Write("ContentType is Empty download was not fine !!!!!"); 
      } 
      if ((response.StatusCode == HttpStatusCode.OK || 
       response.StatusCode == HttpStatusCode.Moved || 
       response.StatusCode == HttpStatusCode.Redirect) && 
       response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase)) 
      { 
       Logger.Write("ContentType is not empty meaning download is fine"); 
       using (Stream inputStream = response.GetResponseStream()) 
       using (Stream outputStream = File.OpenWrite(fileName)) 
       { 
        byte[] buffer = new byte[4096]; 
        int bytesRead; 
        do 
        { 
         bytesRead = inputStream.Read(buffer, 0, buffer.Length); 
         outputStream.Write(buffer, 0, bytesRead); 
        } while (bytesRead != 0); 

       } 
       FinishWebRequest(); 
      } 
      else 
      { 
       timer1.Stop(); 
       timer3.Start(); 
      } 
     } 

在此方法中不知何故,我想calcuate文件的下載進度,並報告到Form1 consturctor:

在Form1構造我所做的:

fileDownloadRadar(remote_image_on_server, combinedTemp); 
splash.UpdateProgressBar(

UpdateProgressBar應該接受int。 在形式飛濺我更新progressBar1我有那裏。

我的問題是如何在fileDownloadRadar中實時計算下載進度以及如何將其報告給splash.UpdateProgressBar方法?

回答