2014年3月8日 星期六

vbnet Net.WebClient downfile

引用來源#1
--
Public Class Form1
    Friend WithEvents MyWebClient As New Net.WebClient

    Private Sub MyWebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles MyWebClient.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyWebClient.DownloadFileAsync(New Uri("http://myweb.com/myfile.zip"), "C:\myfile.zip")
    End Sub
End Class

參考引用來源#2
--
Dim wc As System.Net.WebClient
    Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
        wc = New System.Net.WebClient()
        AddHandler wc.DownloadProgressChanged, AddressOf OnDownloadProgressChanged
        AddHandler wc.DownloadFileCompleted, AddressOf OnFileDownloadCompleted
        wc.DownloadFileAsync(New Uri("http://url.com/file.zip";), "C:\Downloads\file.zip")

    End Sub
    Private Sub OnDownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)

        Dim totalSize As Long = e.TotalBytesToReceive
        Dim downloadedBytes As Long = e.BytesReceived
        Dim percentage As Integer = e.ProgressPercentage
        'Put your progress UI here, you can cancel download by uncommenting the line below
        'wc.CancelAsync()

    End Sub
    Private Sub OnFileDownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        If e.Cancelled Then
            'Cancelled
        ElseIf Not e.Error Is Nothing Then
            'Error occured
        Else
            'File Downloaded Successfuly
        End If

    End Sub

引用來源#3
--
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wClient As New WebClient
        AddHandler wClient.DownloadProgressChanged, AddressOf DownloadProgressChanged
        AddHandler wClient.DownloadStringCompleted, AddressOf DownloadStringCompleted
        wClient.DownloadStringAsync(New Uri(TextBox1.Text))
    End Sub

    Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub DownloadStringCompleted(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
        Dim wClient As WebClient = DirectCast(sender, WebClient)
        RemoveHandler wClient.DownloadProgressChanged, AddressOf DownloadProgressChanged
        RemoveHandler wClient.DownloadStringCompleted, AddressOf DownloadStringCompleted

        ProgressBar1.Value = 0

        If e.Error IsNot Nothing Then
            MessageBox.Show(e.Error.Message)
        ElseIf e.Cancelled Then
            MessageBox.Show("Download cancelled by the user")
        Else
            ' e.Result contains the downloaded string.
            MessageBox.Show(e.Result)
        End If
    End Sub
 

沒有留言:

張貼留言