PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms 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
 1Imports System.Net
 2Imports System.IO
 3Imports System.Threading
 4
 5Public Class DownloadFileTask
 6    Inherits ProgressForm.Task
 7
 8    Private FHref As String = String.Empty
 9
10    Private FResult As Byte() = Nothing
11
12    Public ReadOnly Property DownloadedContents() As Byte()
13        Get
14            Return FResult
15        End Get
16    End Property
17
18    Public Sub New(ByVal href As String)
19        FHref = href
20    End Sub
21
22    Public Overrides Sub Perform()
23
24        Try
25
26            ' Download the file
27            Dim Request As HttpWebRequest = Net.FileWebRequest.Create(FHref)
28            Dim Response As HttpWebResponse = Request.GetResponse
29
30            ' Get the data
31            Dim Data(Response.ContentLength - 1) As Byte
32            Dim Stream As Stream = Response.GetResponseStream()
33            Dim StartPosition As Integer = 0
34            Do
35
36                Dim BytesRead As Integer = Stream.Read(Data, StartPosition, Math.Min(Response.ContentLength - StartPosition, 4096))
37                StartPosition += BytesRead
38                MyBase.RaiseStatusChange((StartPosition / Response.ContentLength) * 100, CInt(StartPosition / 1024).ToString + " KB downloaded.")
39
40            Loop Until StartPosition = Response.ContentLength OrElse MyBase.CancelRequested = True
41            Stream.Close()
42            FResult = Data
43
44        Catch ex As Exception
45            MsgBox("Could not install that translation:" + Environment.NewLine + ex.Message, MsgBoxStyle.Exclamation)
46        End Try
47
48    End Sub
49
50End Class