/Source Code/PowerSong/Export and Import/DownloadFileTask.vb
Visual Basic | 50 lines | 34 code | 14 blank | 2 comment | 0 complexity | 6187711eb7559e5cb623ebbc70631f37 MD5 | raw file
- Imports System.Net
- Imports System.IO
- Imports System.Threading
-
- Public Class DownloadFileTask
- Inherits ProgressForm.Task
-
- Private FHref As String = String.Empty
-
- Private FResult As Byte() = Nothing
-
- Public ReadOnly Property DownloadedContents() As Byte()
- Get
- Return FResult
- End Get
- End Property
-
- Public Sub New(ByVal href As String)
- FHref = href
- End Sub
-
- Public Overrides Sub Perform()
-
- Try
-
- ' Download the file
- Dim Request As HttpWebRequest = Net.FileWebRequest.Create(FHref)
- Dim Response As HttpWebResponse = Request.GetResponse
-
- ' Get the data
- Dim Data(Response.ContentLength - 1) As Byte
- Dim Stream As Stream = Response.GetResponseStream()
- Dim StartPosition As Integer = 0
- Do
-
- Dim BytesRead As Integer = Stream.Read(Data, StartPosition, Math.Min(Response.ContentLength - StartPosition, 4096))
- StartPosition += BytesRead
- MyBase.RaiseStatusChange((StartPosition / Response.ContentLength) * 100, CInt(StartPosition / 1024).ToString + " KB downloaded.")
-
- Loop Until StartPosition = Response.ContentLength OrElse MyBase.CancelRequested = True
- Stream.Close()
- FResult = Data
-
- Catch ex As Exception
- MsgBox("Could not install that translation:" + Environment.NewLine + ex.Message, MsgBoxStyle.Exclamation)
- End Try
-
- End Sub
-
- End Class