2

我有这段代码,如何将进度条合并到其中?

 Private Sub btn_upload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_upload.Click

    Dim filePath As String = lbl_file.Text
    Dim slashPosition As Integer = filePath.LastIndexOf("\")
    Dim filenameOnly As String = filePath.Substring(slashPosition + 1)


    Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://.com/public_html/windows/" & filenameOnly), System.Net.FtpWebRequest)
    request.Credentials = New System.Net.NetworkCredential("", "")
    request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    Dim path As String = lbl_file.Text

    Dim file() As Byte = System.IO.File.ReadAllBytes(path)

    Dim strz As System.IO.Stream = request.GetRequestStream()

    strz.Write(file, 0, file.Length)


    strz.Close()
    strz.Dispose()

    MsgBox(path)




End Sub
4

1 回答 1

4

您可以读取和写入设定大小的块中的字节,然后根据总字节数计算百分比,而不是一次读取所有字节然后一次全部上传。作为一个简单的示例,您可以通过以下方式更新每发送一千字节的上传进度。

For offset as Integer = 0 to File.Length Step 1024
    ProgressBar1.Value = CType(offset * ProgressBar1.Maximum / File.Length, Integer)
    Dim chunkSize as Integer = File.Length - offset - 1
    If chunkSize > 1024 Then chunkSize = 1024
    strz.Write(file, offset, chunkSize)
Next
ProgressBar1.Value = ProgressBar1.Maximum
于 2012-05-11T18:22:48.680 回答