c# winform下载文件并显示进度条(保存路径可选择另存为)
开发工具 VS2010
功能实现
填写下载地址URL,点击另存为按钮,打开另存为对话框,可以选择保存路径,点击下载按钮进行下载,下载过程显示下载百分比和进度条。
窗体
组件 label TextBox Button progressBar saveFileDialog
获取saveFileDialog1文件名和路径
Path.GetDirectoryName(saveFileDialog1.FileName) //获取文件名(不包括路径) Path.GetFileName(saveFileDialog1.FileName);//获取文件路径
下载功能实现
/// <summary> /// c# .net下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// /// <param name="Filename">下载后的存放地址</param> /// <param name="Prog">用于显示的进度条</param> /// public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) { if (URL == "") { MessageBox.Show("URL为空","消息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream 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; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label1.Text = "当前下载进度" + percent.ToString("00.00") + "%"; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 //下载完成后,弹出提示对话框 if (percent == 100) { MessageBox.Show("下载完成", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } so.Close(); st.Close(); } catch (System.Exception ee) { //throw; MessageBox.Show(ee.Message, "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
效果
完整代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;//需添加的IO引用 namespace downloadFilePro { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //打开另存为对话框 private void btnSave_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { textBox2.Text = Path.GetDirectoryName(saveFileDialog1.FileName) + @"\" + Path.GetFileName(saveFileDialog1.FileName);//获取文件路径 } } //下载按钮 private void btnDown_Click(object sender, EventArgs e) { //textBox1存放URL地址,textBox2存放另存为路径 DownloadFile(textBox1.Text, textBox2.Text, progressBar1, label1); } /// <summary> /// c#,.net 下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// /// <param name="Filename">下载后的存放地址</param> /// <param name="Prog">用于显示的进度条</param> /// public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) { if (URL == "") { MessageBox.Show("URL为空","消息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream 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; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label1.Text = "当前下载进度" + percent.ToString("00.00") + "%"; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 if (percent == 100) { MessageBox.Show("下载完成", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } so.Close(); st.Close(); } catch (System.Exception ee) { //throw; MessageBox.Show(ee.Message, "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }