ผมใช้ VS 2012 เขียนด้วยภาษา VB.NET
ไฟล์เวอร์ชั่นจาก http://127.0.0.1/autoupdate/version.txt มันก็โหลดได้นะครับ
แต่พอมาโหลด http://127.0.0.1/autoupdate/download.php ที่ Header ไว้เป็น plain มันโหลดไม่ได้อะครับ
พอกด Pause ไว้มันก็ขึ้นหน้า No Source Available ขึ้นมา
รูปภาพ:
Code (VB.NET)
Imports System.Threading
Imports System.ComponentModel
Imports System.IO
Imports System.Net
Imports System.Data
Public Class Form1
Public Shared DownloadStats As Boolean = True
Public Shared TotalFile As Integer = 0
Public Shared TotalDownload As Integer = 0
Public Shared ProcessFileMD As Integer = 0
Public Shared ProcessFileAllMD As Integer = 0
Public Shared PercentDownload As Integer = 0
Public Shared DownloadFileInfo As String()
Public Shared DownloadFileName As String = ""
Public Shared DownloadFileMB As Integer = 0
Public Shared DownloadFileTotalMB As Integer = 0
Public Shared Path As String = ""
Public Shared DownloadUrl As String = "http://127.0.0.1/autoupdate/download.php"
Public Shared VersionUrl As String = "http://127.0.0.1/autoupdate/version.txt"
Dim whereToSave As String 'Where the program save the file
Dim whereToDownload As String 'Where the program save the file
Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TotalFile = 1
TotalDownload = 0
DownloadFileName = "กำลังรับค่าไฟล์อัพเดรต"
Me.Button2.Enabled = False
Me.Button2.Text = "Updating..."
Me.whereToDownload = DownloadUrl
Me.whereToSave = My.Application.Info.DirectoryPath & "\downloaded.dll"
Me.BackgroundWorker1.RunWorkerAsync()
Do
If (DownloadStats = True And Me.BackgroundWorker1.IsBusy = False) Then
Dim ListDownload() As String = File.ReadAllLines(My.Application.Info.DirectoryPath & "\downloaded.dll")
TotalFile = ListDownload.Length
TotalDownload = 0
Thread.Sleep(100)
For Line As Integer = 0 To (ListDownload.Length - 1)
Dim FileInfo_Downloads() As String = ListDownload(Line).Split(",")
If (Not System.IO.Directory.Exists(FileInfo_Downloads(1))) Then
Dim FolderCreating() As String = FileInfo_Downloads(1).Split("\")
Dim FolderWillCreate As String = ""
For FolderCount As Integer = 0 To (FolderCreating.Length - 2)
FolderWillCreate = FolderWillCreate + "\" + FolderCreating(FolderCount)
Next
System.IO.Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\bin\" & FolderWillCreate)
End If
Do
If (DownloadStats = True And Me.BackgroundWorker1.IsBusy = False) Then
Thread.Sleep(10)
DownloadFileName = DownloadFileName = FileInfo_Downloads(1).Split("\")(FileInfo_Downloads.Length - 1)
Me.whereToDownload = FileInfo_Downloads(0)
Me.whereToSave = My.Application.Info.DirectoryPath & "\bin\" & FileInfo_Downloads(1)
Me.BackgroundWorker1.RunWorkerAsync()
Exit Do
End If
Loop
Next
End If
Exit Do
Loop
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Minecraft.Start("phoomin2012")
End Sub
Public Sub DownloadComplete(ByVal cancelled As Boolean)
TotalDownload = TotalDownload + 1
'Me.ProgressBar1.Maximum = TotalFile
Me.ProgressBar1.Value = (TotalDownload / TotalFile * 100)
'Me.ProgressBar2.Maximum = DownloadFileTotalMB
'Me.ProgressBar2.Value = DownloadFileMB
Me.ProgressBar2.Value = PercentDownload
Me.Label1.Text = "ทั้งหมด " & TotalDownload.ToString & "/" & TotalFile.ToString & " (100%)"
Me.Label2.Text = "ไฟล์ขนาด " & Math.Round((DownloadFileMB / 1024 / 1024), 2) & " MB / " & Math.Round((DownloadFileTotalMB / 1024 / 1024), 2) & " MB (100%) ความเร็วดาวห์โหลด : หยุดการดาวห์โหลด"
Form1.DownloadStats = True
End Sub
Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Dim speeddownload As String = ""
If speed = -1 Then
speeddownload = "กำลังคำนวณ..."
Else
speeddownload = "" & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.Label3.Text = "ชื่อไฟล์ : " & DownloadFileName
Me.Label1.Text = "ทั้งหมด " & TotalDownload.ToString & "/" & TotalFile.ToString & " ( " & (TotalDownload / TotalFile * 100) & "%)"
Me.Label2.Text = "ไฟล์ขนาด " & Math.Round((position / 1024 / 1024), 2) & " MB / " & Math.Round((length / 1024 / 1024), 2) & " MB (" & percent & "%)" & _
" ความเร็วดาวห์โหลด : " & speeddownload & " | " & speed
DownloadFileMB = position
DownloadFileTotalMB = length
PercentDownload = percent
'Me.Label5.Text = "Downloading: " & Me.txtFileName.Text
Me.ProgressBar1.Value = (TotalDownload / TotalFile * 100)
Me.ProgressBar2.Value = percent
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Creating the request and getting the response
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try 'Checks if the file exist
theRequest = WebRequest.Create(Me.whereToDownload)
theResponse = theRequest.GetResponse
Catch ex As Exception
MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
"1) File doesn't exist" & ControlChars.CrLf & _
"2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
MsgBox(ex.Message)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)
Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
'Replacement for Stream.Position (webResponse stream doesn't support seek)
Dim nRead As Integer
'To calculate the download speed
Dim speedtimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then 'If user abort download
Exit Do
End If
speedtimer.Start()
Dim readBytes(4095) As Byte
Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Short
Do
length = theResponse.ContentLength
If Not length = -1 Then
percent = (nRead * 100) / length
Exit Do
End If
Loop
Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
speedtimer.Stop()
readings += 1
If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
speedtimer.Reset()
readings = 0
End If
Loop
'Close the streams
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Me.whereToSave)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(completeDelegate, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TotalFile = 1
DownloadFileName = "ตรวจสอบเวอร์ชั่น"
Me.whereToDownload = VersionUrl
Me.whereToSave = My.Application.Info.DirectoryPath & "\versions.dll"
Me.BackgroundWorker1.RunWorkerAsync()
Do
If (DownloadStats = True) Then
Dim ListVersion() As String = File.ReadAllLines(My.Application.Info.DirectoryPath & "\versions.ver")
If Not My.Settings.Version = ListVersion(0) Then
MsgBox("มีการอัพเดรตใหม่!!" & ControlChars.CrLf & _
"เวอร์ชั่นปัจจุบัน : " & My.Settings.Version & ControlChars.CrLf & _
"เวอร์ชั่นใหม่ : " & ListVersion(0).ToString)
Me.Button2.Enabled = True
End If
Exit Do
End If
Loop
End Sub
End Class
Tag : .NET, VBScript, CakePHP, VB.NET, VS 2012 (.NET 4.x)