上传进度条以进行文件上传

时间:2012-05-11 18:16:08

标签: vb.net

我有这个代码,如何将进度条纳入其中?

 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

1 个答案:

答案 0 :(得分: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