PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/PowerSong/Export and Import/DownloadFileTask.vb

#
Visual Basic | 50 lines | 34 code | 14 blank | 2 comment | 0 complexity | 6187711eb7559e5cb623ebbc70631f37 MD5 | raw file
  1. Imports System.Net
  2. Imports System.IO
  3. Imports System.Threading
  4. Public Class DownloadFileTask
  5. Inherits ProgressForm.Task
  6. Private FHref As String = String.Empty
  7. Private FResult As Byte() = Nothing
  8. Public ReadOnly Property DownloadedContents() As Byte()
  9. Get
  10. Return FResult
  11. End Get
  12. End Property
  13. Public Sub New(ByVal href As String)
  14. FHref = href
  15. End Sub
  16. Public Overrides Sub Perform()
  17. Try
  18. ' Download the file
  19. Dim Request As HttpWebRequest = Net.FileWebRequest.Create(FHref)
  20. Dim Response As HttpWebResponse = Request.GetResponse
  21. ' Get the data
  22. Dim Data(Response.ContentLength - 1) As Byte
  23. Dim Stream As Stream = Response.GetResponseStream()
  24. Dim StartPosition As Integer = 0
  25. Do
  26. Dim BytesRead As Integer = Stream.Read(Data, StartPosition, Math.Min(Response.ContentLength - StartPosition, 4096))
  27. StartPosition += BytesRead
  28. MyBase.RaiseStatusChange((StartPosition / Response.ContentLength) * 100, CInt(StartPosition / 1024).ToString + " KB downloaded.")
  29. Loop Until StartPosition = Response.ContentLength OrElse MyBase.CancelRequested = True
  30. Stream.Close()
  31. FResult = Data
  32. Catch ex As Exception
  33. MsgBox("Could not install that translation:" + Environment.NewLine + ex.Message, MsgBoxStyle.Exclamation)
  34. End Try
  35. End Sub
  36. End Class