[VB2010]如何使用URL(網址)下載檔案?

利用Downloader類別製作簡易的下載器

Downloader可以應用在許多地方,至於要怎麼使用就要看各位程式設計者是怎麼想的囉!在此提供一個簡易下載器的程式碼,請自行照以下程式碼提供的方式佈置表單控制項元件,然後將程式碼貼入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'建立1個Form,並命名為FileDownloadForm
'建立15個Label,並分別命名為URLLabel、SaveFolderPathLabel、SaveFileNameLabel、SaveFullPathLabel、TimeLabel、LastTimeLabel、DownloadProgressLabel、SpeedLabelExplain、SpeedLabel、BytesReceivedLabelExplain、BytesReceivedLabel、LastBytesLabelExplain、LastBytesLabel、TotleSizeLabelExplain、TotleSizeLabel
'建立4個TextBox,並分別命名為URLTextBox、SaveFolderPathTextBox、SaveFileNameTextBox、SaveFullPathTextBox
'建立4個Button,並分別命名為SaveFolderPathButton、SaveFullPathButton、DownloadStartButton、DownloadStopButton
'建立1個ProgressBar,並命名為DownloadProgressBar
'建立1個Timer,並命名為DownloadTimer
 
Public Class FileDownloadForm
 
    Dim DownloadManager As New Downloader("http://dlce.antivir.com/package/wks_avira/win32/zhtw/pecl/avira_antivir_personal_zhtw.exe") '建立一個Downloader實體
 
    Private Sub FileDownloadForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '載入表單時
        '-------------------------編排控制項元件(若想自訂的話請自行將這段刪除或是修改)'-------------------------
 
        Me.Text = "下載器"
        Me.Size = New Size(370, 305)
        Me.Location = New Point((Screen.PrimaryScreen.Bounds.Width / 2) - (Me.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Height / 2) - (Me.Size.Height / 2)) '位於螢幕中央
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
        Me.MaximizeBox = False
 
        URLLabel.Text = "輸入網址:"
        URLLabel.Location = New Point(12, 15)
 
        SaveFolderPathLabel.Text = "儲存目錄:"
        SaveFolderPathLabel.Location = New Point(12, 50)
 
        SaveFileNameLabel.Text = "儲存檔名:"
        SaveFileNameLabel.Location = New Point(12, 84)
 
        SaveFullPathLabel.Text = "儲存路徑:"
        SaveFullPathLabel.Location = New Point(12, 116)
 
        TimeLabel.Text = "已使用:"
        TimeLabel.Location = New Point(12, 164)
 
        LastTimeLabel.Text = "尚需:"
        LastTimeLabel.Location = New Point(24, 176)
 
        SpeedLabelExplain.Text = "速度:"
        SpeedLabelExplain.Location = New Point(223, 188)
 
        BytesReceivedLabelExplain.Text = "接收:"
        BytesReceivedLabelExplain.Location = New Point(223, 208)
 
        LastBytesLabelExplain.Text = "剩餘:"
        LastBytesLabelExplain.Location = New Point(223, 228)
 
        TotleSizeLabelExplain.Text = "合計:"
        TotleSizeLabelExplain.Location = New Point(223, 248)
 
        DownloadProgressLabel.AutoSize = False
        DownloadProgressLabel.Size = New Size(100, 21)
        DownloadProgressLabel.TextAlign = ContentAlignment.MiddleRight
        DownloadProgressLabel.Text = "000.00%"
        DownloadProgressLabel.Location = New Point(245, 164)
 
        SpeedLabel.AutoSize = False
        SpeedLabel.Size = New Size(75, 19)
        SpeedLabel.TextAlign = ContentAlignment.MiddleRight
        SpeedLabel.Text = "000.00 KB/s"
        SpeedLabel.Location = New Point(270, 185)
 
        BytesReceivedLabel.AutoSize = False
        BytesReceivedLabel.Size = New Size(75, 19)
        BytesReceivedLabel.TextAlign = ContentAlignment.MiddleRight
        BytesReceivedLabel.Text = "000.00 MB"
        BytesReceivedLabel.Location = New Point(270, 205)
 
        LastBytesLabel.AutoSize = False
        LastBytesLabel.Size = New Size(75, 19)
        LastBytesLabel.TextAlign = ContentAlignment.MiddleRight
        LastBytesLabel.Text = "000.00 MB"
        LastBytesLabel.Location = New Point(270, 225)
 
        TotleSizeLabel.AutoSize = False
        TotleSizeLabel.Size = New Size(75, 19)
        TotleSizeLabel.TextAlign = ContentAlignment.MiddleRight
        TotleSizeLabel.Text = "000.00 MB"
        TotleSizeLabel.Location = New Point(270, 245)
 
        URLTextBox.Size = New Size(262, 22)
        URLTextBox.Location = New Point(83, 9)
 
        SaveFolderPathTextBox.Size = New Size(181, 22)
        SaveFolderPathTextBox.Location = New Point(83, 43)
 
        SaveFileNameTextBox.Size = New Size(181, 22)
        SaveFileNameTextBox.Location = New Point(83, 77)
 
        SaveFullPathTextBox.Size = New Size(181, 22)
        SaveFullPathTextBox.Location = New Point(83, 110)
 
        SaveFolderPathButton.Text = "瀏覽"
        SaveFolderPathButton.Size = New Size(75, 23)
        SaveFolderPathButton.Location = New Point(270, 42)
 
        SaveFullPathButton.Text = "瀏覽"
        SaveFullPathButton.Size = New Size(75, 23)
        SaveFullPathButton.Location = New Point(270, 109)
 
        DownloadStartButton.Text = "開始下載"
        DownloadStartButton.Size = New Size(75, 37)
        DownloadStartButton.Location = New Point(14, 205)
 
        DownloadStopButton.Text = "停止下載"
        DownloadStopButton.Size = New Size(75, 37)
        DownloadStopButton.Location = New Point(127, 205)
 
        DownloadProgressBar.Size = New Size(331, 23)
        DownloadProgressBar.Location = New Point(14, 138)
 
        '--------------------------------------------------------------------------------------------------------
 
 
        DownloadManager.TimerGate(DownloadTimer, 1000) '將Timer元件加入DownloadManager,用來觸發事件。每1秒(1000毫秒)鐘變化一次
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub FileDownloadForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing '關閉表單時
        DownloadManager.StopDownloading() '中斷下載
    End Sub
 
    Private Sub SaveFolderPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFolderPathButton.Click '「瀏覽」「儲存目錄」
        DownloadManager.SaveFolderPath = DownloadManager.ChoseFolderPath '直接開啟對話方塊選取資料夾
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub SaveFullPathButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFullPathButton.Click '「瀏覽」「儲存路徑」
        DownloadManager.SaveFullPath = DownloadManager.ChoseFullPath  '直接開啟對話方塊設定路徑及檔名
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub SaveFileNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFileNameTextBox.TextChanged '當檔名輸入方塊的內容更變時
        DownloadManager.SaveFileName = SaveFileNameTextBox.Text '將更變後的內容存入DownloadManager
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub SaveFolderPathTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFolderPathTextBox.TextChanged '當儲存目錄輸入方塊的內容更變時
        DownloadManager.SaveFolderPath = SaveFolderPathTextBox.Text '將更變後的內容存入DownloadManager
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub SaveFullPathTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFullPathTextBox.TextChanged '當儲存路徑輸入方塊的內容更變時
        DownloadManager.SaveFullPath = SaveFullPathTextBox.Text '將更變後的內容存入DownloadManager
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub URLTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles URLTextBox.TextChanged '當網址輸入方塊的內容更變時
        DownloadManager.TargetURL = URLTextBox.Text '將更變後的內容存入DownloadManager
        Call ReFlash() '重新整理表單
    End Sub
 
    Private Sub DownloadStartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadStartButton.Click '開始下載
        DownloadStartButton.Enabled = False '禁用開始下載按鈕
 
        '初始化
        DownloadManager.TargetURL = URLTextBox.Text
        DownloadManager.SaveFullPath = SaveFullPathTextBox.Text
 
        DownloadManager.StartDownload() '開始下載
 
        While DownloadManager.CheckDownloading '如果正在下載中,就永遠執行迴圈
            Application.DoEvents() '停頓(多工)
        End While
 
        Select Case DownloadManager.GetStatus '取得狀態傳回值
            Case -2 '尚未下載
                MsgBox("尚未下載", MsgBoxStyle.Exclamation)
            Case -1 '下載中
                MsgBox("下載中", MsgBoxStyle.Information)
            Case 0 '下載失敗
                MsgBox("下載失敗", MsgBoxStyle.Critical)
            Case 1 '下載成功
                DownloadProgressBar.Value = 100
                DownloadProgressLabel.Text = "100%"
                MsgBox("下載成功。使用時間:" & DownloadManager.FormatTime(DownloadManager.GetDownloadedTime), MsgBoxStyle.Information)
            Case 2
                MsgBox("中斷下載", MsgBoxStyle.Exclamation)
        End Select
 
        DownloadStartButton.Enabled = True '啟用開始下載按鈕
    End Sub
 
    Private Sub DownloadStopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadStopButton.Click '停止下載
        DownloadManager.StopDownloading() '停止下載
    End Sub
 
    Private Sub ReFlash() '重新整理表單
        URLTextBox.Text = DownloadManager.TargetURL '取得URL
        SaveFolderPathTextBox.Text = DownloadManager.SaveFolderPath '取得儲存目錄
        SaveFileNameTextBox.Text = DownloadManager.SaveFileName '取得儲存檔名
        SaveFullPathTextBox.Text = DownloadManager.SaveFullPath '取得完整儲存路徑
    End Sub
 
 
    Private Sub DownloadTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadTimer.Tick 'Timer事件
        DownloadProgressBar.Value = DownloadManager.GetProgressPercentage '改變進度列
        TimeLabel.Text = "已使用:" & DownloadManager.FormatTime(DownloadManager.GetDownloadingTime) '顯示已經過時間
        LastTimeLabel.Text = "尚需:" & DownloadManager.FormatTime(DownloadManager.GetLastTime) '顯示剩餘時間
        DownloadProgressLabel.Text = DownloadManager.GetDoubleProgressPercentage(2) & "%" '顯示進度百分比
        SpeedLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetEverySecondSpead, 2) & "/s" '顯示速度
        BytesReceivedLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetBytesReceived, 2) '顯示已接收位元組數
        LastBytesLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetLastBytes, 2) '顯示剩餘接收位元組數
        TotleSizeLabel.Text = DownloadManager.FormatBytes(DownloadManager.GetTotalBytesToReceive, 2) '顯示目標檔案位元組數
    End Sub
End Class

執行結果如下:

文章分類:VB.net|標籤:, , , ,

迴響已關閉