VB.Net 使用FTPWebRequest下载进度条

Public Sub DFile(ByVal Str_Path As String, ByVal Local_Path As String, ByVal Str_UserName As String, ByVal Str_Password As String)

        Dim URI As String = Str_Path
        Dim ftp As System.Net.FtpWebRequest = _
            CType(FtpWebRequest.Create(URI), FtpWebRequest)
        ftp.Credentials = New _
    System.Net.NetworkCredential(Str_UserName, Str_Password)

        ftp.KeepAlive = False
        ftp.UseBinary = True
        ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

        Using response As System.Net.FtpWebResponse = _
      CType(ftp.GetResponse, System.Net.FtpWebResponse)
            '创建进度条,把他加入FORM中
            'Dim pb As New ProgressBar
            '设置最小值
            ProgressBar1.Minimum = 0
            Using responseStream As IO.Stream = response.GetResponseStream

                ''设置最大值
                ProgressBar1.Maximum = GetFileSize("ftp://1.1.1.1/xx.zip", "用户名", "******")
                'loop to read & write to file
                Using fs As New IO.FileStream(Local_Path, IO.FileMode.Create)

                    Dim buffer(2047) As Byte
                    Dim read As Integer = 0
                    Do
                        read = responseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)

                        '每次读取的长度
                        If ProgressBar1.Maximum > (ProgressBar1.Value + 2047) Then
                            ProgressBar1.Value += buffer.Length
                        Else
                            ProgressBar1.Value = ProgressBar1.Maximum
                        End If

                    Loop Until read = 0 'see Note(1)
                    responseStream.Close()
                    fs.Flush()
                    fs.Close()
                End Using
                responseStream.Close()
            End Using
            response.Close()
        End Using

        MsgBox("下载完成", MsgBoxStyle.OkOnly + 48, "提示")
    End Sub
    Public Function GetFileSize(ByVal filename As String, ByVal Str_UserName As String, ByVal Str_Password As String) As Long
        Dim URI As String = filename
        Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
        ftp.Credentials = New System.Net.NetworkCredential(Str_UserName, Str_Password)
        ftp.Method = WebRequestMethods.Ftp.GetFileSize
        Dim response As FtpWebResponse = ftp.GetResponse
        GetFileSize = response.ContentLength
        response.Close()
    End Function

  • 0
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

tk600158

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值