C# 下載帶進度條程式碼(普通進度條)

衣舞晨風發表於2015-05-11
<span style="font-size:14px;">     </span><pre name="code" class="csharp"><span style="font-size:14px;">        /// <summary>  
        /// 下載帶進度條程式碼(普通進度條)  
        /// </summary>  
        /// <param name="URL">網址</param>  
        /// <param name="Filename">下載後檔名為</param>  
        /// <param name="Prog">報告進度的處理(第一個引數:總大小,第二個引數:當前進度)</param>  
        /// <returns>True/False是否下載成功</returns>  
        public static bool DownLoadFile(string URL, string Filename, Action<int, int> updateProgress = null)
        {
            Stream st = null;
            Stream so = null;
             System.Net.HttpWebRequest Myrq =null;
             System.Net.HttpWebResponse myrp = null;
            bool flag = false;
            try
            {
                Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); //從URL地址得到一個WEB請求     
                myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); //從WEB請求得到WEB響應     
                long totalBytes = myrp.ContentLength; //從WEB響應得到總位元組數
                //更新進度
                if (updateProgress != null)
                {
                    updateProgress((int)totalBytes,0);//從總位元組數得到進度條的最大值  
                }
                st = myrp.GetResponseStream(); //從WEB請求建立流(讀)     
                so = new System.IO.FileStream(Filename, System.IO.FileMode.Create); //建立檔案流(寫)     
                long totalDownloadedByte = 0; //下載檔案大小     
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length); //讀流     
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte; //更新檔案大小     
                    Application.DoEvents();
                    so.Write(by, 0, osize); //寫流     
                    //更新進度
                    if (updateProgress != null)
                    {
                        updateProgress((int)totalBytes, (int)totalDownloadedByte);//更新進度條 
                    }
                    osize = st.Read(by, 0, (int)by.Length); //讀流     
                }
                //更新進度
                if (updateProgress != null)
                {
                    updateProgress((int)totalBytes, (int)totalBytes);
                }
                flag= true;
            }
            catch(Exception )
            {
                flag = false;
                throw;
                //return false;
            }
            finally
            {
                if (Myrq != null)
                {
                    Myrq.Abort();//銷燬關閉連線
                }
                if (myrp != null)
                {
                    myrp.Close();//銷燬關閉響應
                }
                if (so != null)
                {
                    so.Close(); //關閉流 
                }
                if (st != null)
                {
                    st.Close(); //關閉流  
                }
            }
            return flag;
        }</span>

呼叫方式一:

<span style="font-size:14px;"> if (FileUpDownload.DownLoadFile("下載檔案的網址", "檔名", new Action<int, int>(
        (int Maximum, int Value)
        =>
        {
            //更新進度條
            progressBar1.Maximum = Maximum;
            progressBar1.Value = Value;
        })
))
{
	//下載檔案後的處理
	
	
}</span>

不傳引數的action寫法:

((Form)form).BeginInvoke(new Action(() =>
            {
                //處理
            })

呼叫方式二:

<span style="font-size:14px;"> </span><pre name="code" class="csharp"><span style="font-size:14px;"> if (FileUpDownload.DownLoadFile("下載檔案的網址", "檔名", UpdateProgressBar))
 {
	//下載檔案後的處理
	
}
////更新進度條
private void UpdateProgressBar(int Maximum, int Value)
{
     progressBar1.Maximum = Maximum;
     progressBar1.Value = Value;
}</span>

小注:呼叫程式碼中的progressBar1是微軟的進度條控制元件

URL示例:http://111.111.111.211/TestService/download/V1.00/1.00_0012.zip


如果要充Windows Server上下載檔案的話,需要在iis中配置,該目錄為可以訪問的,具體配置步驟如下:

1、找到需要下載檔案對應的目錄:


2、在右側找到《目錄瀏覽》:


3、在《目錄瀏覽》上右鍵,點選:開啟該功能


4、可以看到下圖,啟用即可:



相關文章