Hello All,
we (a couple of friend and myself) are currently working on a web browser with a webkit based webengine (like chrome or safari).
but we came across a problem.
the file downloader isn't working correctly, so we made a own download manager.
but that one doesn't work either.
we found the problem. it has to do with the way the browser is downloading the file
for example if we try to download a project from codeproject it downloads normaly but when we try to open th .zip file we downloaded it gives us an error (damaged archive file)
but thats not al if we rename the file frome projectname.zip to projectname.php
it opens properly and it shows the download page of codeproject.
so for an unknow reason the filedownloader is downloading the pgae with the name + extension of the file we actually want
but it only occurs when we download a file from an url (for example
http://www.codeproject.com/KB/IP/MyDownloader/MyDownloader_src.zip[
^] (which is actualy a page))
but when we download a file like
http://dl.bukkit.org/latest-rb/craftbukkit.jar[
^] it works fine and it downloads thecraftbukkit.jar and not "the page"
please help us because it is realy driving us mad
PS. we are using the folowing code
(full downloader.vb file and the part inside main_win)
Main_win
Public Sub WebKitBrowser1_DownloadBegin(Sender As Object, e As WebKit.FileDownloadBeginEventArgs)
Dim time As DateTime = DateTime.Now
Dim format As String = "d/M/yyyy HH:mm"
Dim newItem As New ListViewItem(e.SuggestedFileName)
Downloader.txtUrl.Text = e.Url.ToString
Downloader.txtPath.Text = GetDownloadsPath() + "\" + e.SuggestedFileName
Downloader.Show()
newItem.SubItems.Add(e.Url.ToString)
newItem.SubItems.Add(time.ToString(format))
ListView1.Items.Add(newItem)
My.Settings.Downloads.Add(e.SuggestedFileName + "|" + e.Url.ToString + "|" + time.ToString(format))
My.Settings.Save()
End Sub
downloader.vb
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Net
Imports System.IO
Imports System.Threading
Public Class Downloader
Private thrDownload As Thread
Private strResponse As Stream
Private strLocal As Stream
Private webRequest As HttpWebRequest
Private webResponse As HttpWebResponse
Private Shared PercentProgress As Integer
Private Delegate Sub UpdateProgessCallback(BytesRead As Int64, TotalBytes As Int64)
Private goPause As Boolean = False
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnDownload_Click(sender As Object, e As EventArgs)
If thrDownload IsNot Nothing AndAlso thrDownload.ThreadState = ThreadState.Running Then
MessageBox.Show(Main_Win.rm.GetString("Downloader_Message1"), Main_Win.rm.GetString("Downloader_Message1Title"), MessageBoxButtons.OK, MessageBoxIcon.[Error])
Else
lblProgress.Text = Main_Win.rm.GetString("Downloader_lblProgressStarting")
thrDownload = New Thread(New ParameterizedThreadStart(AddressOf Download))
thrDownload.Start(0)
btnPauseResume.Enabled = True
btnPauseResume.Text = Main_Win.rm.GetString("Downloader_btnPause")
End If
End Sub
Private Sub UpdateProgress(BytesRead As Int64, TotalBytes As Int64)
PercentProgress = Convert.ToInt32((BytesRead * 100) \ TotalBytes)
prgDownload.Value = PercentProgress
lblProgress.Text = Main_Win.rm.GetString("Downloader_lblProgressDownloaded") + ":" + BytesRead + Main_Win.rm.GetString("Downloader_lblProgressOutOf") + TotalBytes + " (" & PercentProgress + "%)"
End Sub
Private Sub Download(startPoint As Object)
Try
Dim startPointInt As Integer = Convert.ToInt32(startPoint)
webRequest = DirectCast(Net.WebRequest.Create(txtUrl.Text), HttpWebRequest)
webRequest.AddRange(startPointInt)
webRequest.Credentials = CredentialCache.DefaultCredentials
webResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Dim fileSize As Int64 = webResponse.ContentLength
strResponse = webResponse.GetResponseStream()
If startPointInt = 0 Then
strLocal = New FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None)
Else
strLocal = New FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None)
End If
Dim bytesSize As Integer = 0
Dim downBuffer As Byte() = New Byte(2047) {}
While (InlineAssignHelper(bytesSize, strResponse.Read(downBuffer, 0, downBuffer.Length))) > 0
strLocal.Write(downBuffer, 0, bytesSize)
Me.Invoke(New UpdateProgessCallback(AddressOf Me.UpdateProgress), New Object() {strLocal.Length, fileSize + startPointInt})
If goPause = True Then
Exit While
End If
End While
Finally
strResponse.Close()
strLocal.Close()
End Try
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs)
thrDownload.Abort()
webResponse.Close()
strResponse.Close()
strLocal.Close()
prgDownload.Value = 0
lblProgress.Text = Main_Win.rm.GetString("Downloader_lblProgressStopped")
btnPauseResume.Enabled = False
End Sub
Private Sub btnPauseResume_Click(sender As Object, e As EventArgs)
If thrDownload IsNot Nothing Then
If btnPauseResume.Text = Main_Win.rm.GetString("Downloader_btnPause") Then
goPause = True
btnPauseResume.Text = Main_Win.rm.GetString("Downloader_btnResume")
webResponse.Close()
strResponse.Close()
strLocal.Close()
thrDownload.Abort()
Else
goPause = False
btnPauseResume.Text = Main_Win.rm.GetString("Downloader_btnPause")
Dim startPoint As Long = 0
If File.Exists(txtPath.Text) Then
startPoint = New FileInfo(txtPath.Text).Length
Else
MessageBox.Show(Main_Win.rm.GetString("Downloader_Message2"), Main_Win.rm.GetString("Downloader_Message2Title"), MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
lblProgress.Text = Main_Win.rm.GetString("Downloader_lblProgressResume")
thrDownload = New Thread(New ParameterizedThreadStart(AddressOf Download))
thrDownload.Start(startPoint)
btnPauseResume.Enabled = True
End If
Else
MessageBox.Show(Main_Win.rm.GetString("Downloader_Message3"), Main_Win.rm.GetString("Downloader_Message3Title"), MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Private Sub Downloader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.lblUrl.Text = Main_Win.rm.GetString("Downloader_lblUrl")
Me.lblPath.Text = Main_Win.rm.GetString("Downloader_lblPath")
Me.lblProgress.Text = Main_Win.rm.GetString("Downloader_lblProgressWait")
Me.btnDownload.Text = Main_Win.rm.GetString("Downloader_btnDownload")
Me.btnStop.Text = Main_Win.rm.GetString("Downloader_btnStop")
Me.btnPauseResume.Text = Main_Win.rm.GetString("Downloader_btnPause")
Me.lblUrl.ForeColor = Color.Black
Me.lblPath.ForeColor = Color.Black
Me.lblProgress.ForeColor = Color.Black
Me.lblUrl.BackColor = Color.Transparent
Me.lblPath.BackColor = Color.Transparent
Me.lblProgress.BackColor = Color.Transparent
Me.btnDownload.ForeColor = Color.Black
Me.btnStop.ForeColor = Color.Black
Me.btnPauseResume.ForeColor = Color.Black
Me.btnDownload.BackColor = Color.Transparent
Me.btnStop.BackColor = Color.Transparent
Me.btnPauseResume.BackColor = Color.Transparent
End Sub
End Class