一段显示下载进度条的下载文件代码 VB.NET Public Shared Sub DownFile(ByVal URL As String, ByVal Filename As String, ByVal Prog As ProgressBar) Dim Myrq As HttpWebRequest = HttpWebRequest.Create(URL) Dim myrp As HttpWebResponse = Myrq.GetResponse Dim totalBytes As Long = myrp.ContentLength Prog.Maximum = totalBytes Dim st As Stream = myrp.GetResponseStream Dim so As Stream = New FileStream(Filename, FileMode.Create) Dim totalDownloadedByte As Long = 0 Dim by(1024) As Byte Dim osize As Integer = st.Read(by, 0, by.Length) While osize > 0 totalDownloadedByte = osize + totalDownloadedByte Application.DoEvents() so.Write(by, 0, osize) Prog.Value = totalDownloadedByte osize = st.Read(by, 0, by.LongLength) End While so.Close() st.Close() End Sub C# public static void DownFile( string URL, string Filename, ProgressBar Prog ) { 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; 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; Application.DoEvents(); so.Write(by, 0, osize); Prog.Value = (int)totalDownloadedByte; osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); } 我没有测试过,希望可以给你点思路!(转发)
public static void DownFile( string URL, string Filename, ProgressBar Prog ) ...{ 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; 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; Application.DoEvents(); so.Write(by, 0, osize); Prog.Value = (int)totalDownloadedByte; osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); }
TO wzq6511(不怕不会,就怕不学!) 你的方法我的几点问题: 1;怎么取得要下载数据的count 下载是按表下载的.当然也得去读数据.不过要取得这个COUNT好象挺难. 2.进度提示要显示的表名要怎么办?
TO paulyjin 和anncesky1 感谢你们提供的方法.还有什么思路吗?
用select count(*) from tablename计算有多少条记录,把所有表的count加起来就是总的count,要显示表名,就要知道当前下载的是哪张表的数据。我在公司的项目中都是这样做的,也算是原创了,不知道有没有别人用过
VB.NET Public Shared Sub DownFile(ByVal URL As String, ByVal Filename As String, ByVal Prog As ProgressBar)
Dim Myrq As HttpWebRequest = HttpWebRequest.Create(URL)
Dim myrp As HttpWebResponse = Myrq.GetResponse
Dim totalBytes As Long = myrp.ContentLength
Prog.Maximum = totalBytes
Dim st As Stream = myrp.GetResponseStream
Dim so As Stream = New FileStream(Filename, FileMode.Create)
Dim totalDownloadedByte As Long = 0
Dim by(1024) As Byte
Dim osize As Integer = st.Read(by, 0, by.Length)
While osize > 0
totalDownloadedByte = osize + totalDownloadedByte
Application.DoEvents()
so.Write(by, 0, osize)
Prog.Value = totalDownloadedByte
osize = st.Read(by, 0, by.LongLength)
End While
so.Close()
st.Close()
End Sub
C#
public static void DownFile( string URL, string Filename, ProgressBar Prog )
{
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;
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;
Application.DoEvents();
so.Write(by, 0, osize);
Prog.Value = (int)totalDownloadedByte;
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
} 我没有测试过,希望可以给你点思路!(转发)
...{
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;
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;
Application.DoEvents();
so.Write(by, 0, osize);
Prog.Value = (int)totalDownloadedByte;
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
}
你的方法我的几点问题:
1;怎么取得要下载数据的count 下载是按表下载的.当然也得去读数据.不过要取得这个COUNT好象挺难.
2.进度提示要显示的表名要怎么办?
感谢你们提供的方法.还有什么思路吗?
http://bbs.msproject.cn/Default.aspx?g=posts&t=410