您好我正在尝试为FTP图片上传添加进度条,虽然我在互联网上查看了很多示例,但我尝试过很多东西,但是我无法使用以下代码。我想要一个进度条来跟踪上传的进度,然后在完成后显示100%。
我的编码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Static r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To 8
Dim idx As Integer = r.Next(0, 35)
sb.Append(s.Substring(idx, 1))
Next
Clipboard.SetText("http://my-site.com/osdf/" + sb.ToString() + ".png")
Using ms As New System.IO.MemoryStream
sc.CaptureDeskTopRectangle(Me.boundsRect).Save(ms, System.Drawing.Imaging.ImageFormat.Png)
''Close()
Using wc As New System.Net.WebClient
AddHandler wc.UploadDataCompleted, AddressOf UploadCompleted
wc.UploadData("ftp://USERNAME:PASSWORD@My-site.com/pic/uploads/" + sb.ToString() + ".png", ms.ToArray())
End Using
MsgBox("File Uploaded", MsgBoxStyle.Information)
End Using
End Sub
我如何使用我的代码执行此操作?
提前致谢,我将非常感谢您的支持。
答案 0 :(得分:0)
更改您的代码并使用WebClient事件,如:
Using wc As New System.Net.WebClient
AddHandler wc.UploadDataCompleted, AddressOf wc_UploadDataCompleted
AddHandler wc.UploadProgressChanged, AddressOf wc_UploadProgressChanged
wc.UploadData("ftp://USERNAME:PASSWORD@My-site.com/pic/uploads/" & sb.ToString() & ".png", ms.ToArray())
End Using
你还需要:
Private Sub wc_UploadDataCompleted(sender As Object, e As System.Net.UploadDataCompletedEventArgs)
MsgBox("File Uploaded", MsgBoxStyle.Information)
End Sub
Private Sub wc_UploadProgressChanged(sender As Object, e As System.Net.UploadProgressChangedEventArgs)
ProgressBar1.Value = e.ProgressPercentage
End Sub
您需要使用“UploadDataAsync”而不是“UploadData”才能看到进度条中的增量或使用带有UploadData方法的BackgroundWorker。