2014年3月8日 星期六

vbnet Download File Asynchronously With ProgressBar (正確沒問題版)

參考引用
--
Private Sub btnStartDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartDownload.Click
    Dim client As WebClient = New WebClient
    AddHandler client.DownloadProgressChanged, AddressOf client_ProgressChanged
    AddHandler client.DownloadFileCompleted, AddressOf client_DownloadCompleted
    client.DownloadFileAsync(New Uri("SomeURL"), "SomePlaceOnHardDrive")
    btnStartDownload.Text = "Download in Progress"
    btnStartDownload.Enabled = False
End Sub

Private Sub client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString())
    Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString())
    Dim percentage As Double = bytesIn / totalBytes * 100

    progressBar.Value = Int32.Parse(Math.Truncate(percentage).ToString())
End Sub

Private Sub client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    MessageBox.Show("Download Complete")
    btnStartDownload.Text = "Start Download"
    btnStartDownload.Enabled = True
End Sub

沒有留言:

張貼留言