下载文件函数

来源:互联网 发布:推荐算法 不重复内容 编辑:程序博客网 时间:2024/05/18 00:10
    '函数名  :DownFile  
    '函数功能:下载文件
    '传入参数:URL网络文件地址,FileName:文件路径,Prog:进度条对象
    '返回值  :无
    '制作人  :zzz
    '制作日期:2007-04-10
    '说  明  :
    Public Sub DownFile(ByVal URL As StringByVal Filename As StringByRef 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(1024As 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