3

目前我有以下功能:

Private Function uploadFile(ByVal fileName As String)
    Dim data() As Byte = IO.File.ReadAllBytes(fileName)
    Dim base64String = System.Convert.ToBase64String(data)
    Dim uploadURL As String = "http://192.168.0.7/upload.php"
    Try
        Using client As New Net.WebClient
            Dim reqParm As New Specialized.NameValueCollection
            reqParm.Add("body", base64String)
            Dim responsebytes = client.UploadValues(uploadURL, "POST", reqParm)
            Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
        End Using
    Catch ex As System.Net.WebException
        MsgBox(ex.Message)
    Finally
            '
    End Try
End Function

这会将 http 请求中的文件内容作为 base64 编码字符串发送。我想添加一个进度条,以便监控进度。使用 Java,我可以通过增加一个字节发送计数器来实现这一点,因为我一次向请求的输出流添加一个字节。我是否有与 VB.net 相同的选项,或者可能有其他选项?感谢您的任何想法

4

1 回答 1

1

您可以使用UploadValuesAsync和收听事件处理程序UploadProgressChangedUploadValuesCompleted.

示例代码:

Private Sub UploadProgressChanged(ByVal sender As Object, ByVal e As Net.UploadProgressChangedEventArgs)
    ' The progress is updated
    ProgressBar1.Value = e.ProgressPercentage
End Sub

Private Sub UploadValuesCompleted(ByVal sender As Object, ByVal e As Net.UploadValuesCompletedEventArgs)
    Dim responsebytes = e.Result
    Dim responsebody = (New System.Text.UTF8Encoding).GetString(responsebytes)
    ' Do whatever you want
    MsgBox(responsebody)
End Sub

Private Function uploadFile(ByVal fileName As String) As Boolean
    Dim data() As Byte = IO.File.ReadAllBytes(fileName)
    Dim base64String = System.Convert.ToBase64String(data)
    Dim uploadURL As String = "http://192.168.0.7/upload.php"
    Try
        Using client As New Net.WebClient
            Dim reqParm As New Specialized.NameValueCollection
            reqParm.Add("body", base64String)
            AddHandler client.UploadProgressChanged, AddressOf UploadProgressChanged
            AddHandler client.UploadValuesCompleted, AddressOf UploadValuesCompleted
            client.UploadValuesAsync(New System.Uri(uploadURL), "POST", reqParm)
        End Using
    Catch ex As System.Net.WebException
        MsgBox(ex.Message)
    Finally
        '
    End Try
    Return True
End Function
于 2012-11-11T03:44:43.427 回答