Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello

I've made a web(kit) browser with a build in downloader, but when i'm trying to download files like:

www.some-url.com/download.php?file=some_filenam.txt it doesn't download the file but it downloads the page download.php and saves it as some_filename.txt

how can i fix this problem

code im using to download:
VB
webClient1.DownloadFileAsync(New Uri("www.some-url.com/download.php?file=some_filenam.txt"), Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "/some_filename.txt")



Bart de Lange
Posted
Comments
Sergey Alexandrovich Kryukov 24-Mar-13 23:43pm    
It's clear what is a downloader, but what is "downloader for web browser"?
And what it your application type? If this is ASP.NET, you cannot use such folder for a target.
—SA
Bart de Lange 25-Mar-13 2:44am    
A file downloader for a web browser i've made
Vb.net and winforms (tags)
And no it issnt this folder but the downloads folder,
This is just the way i'm downloading a file.

(I dont have acces to my pc at the moment
But i'm gettimg the downloads folder with a function)
Sergey Alexandrovich Kryukov 25-Mar-13 11:27am    
What is "for a Web browser"? What's the role of a browser?
—SA
Bart de Lange 25-Mar-13 14:58pm    
It's a webbrowser i've made in vb.net and openwebkitsharp
Now i've made a custom downloader that shows up when my browser requests a downloadable link
Sergey Alexandrovich Kryukov 25-Mar-13 15:23pm    
Got it, thank you for clarification.
OK, what's the problem, exactly?
And the more flexible download method is using HttpWebRequest. You need to use a separate thread for downloading, of course.
—SA

Try to use function URLDownloadToFile

http://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx[^]
 
Share this answer
 
Comments
Bart de Lange 25-Mar-13 14:53pm    
Can you give me an example of this,
Becaus i don't understand the code
Style-7 25-Mar-13 14:59pm    
http://forum.pellesc.de/index.php?topic=3253.0

use Google
Bart de Lange 27-Mar-13 9:50am    
The URLdownloadToFile is for ini but i need to download a file from a website given on a download request from my webbrowser control which is running on a webkit engine
and that doesn't work here
Here is the full code for the download form

VB
Imports System.Net
Public Class downloader
    Public whereToSave As String
    Public dir As String
    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)
    Public Sub DownloadComplete(ByVal cancelled As Boolean)
        Me.txtFileName.Enabled = True
        Me.btnDownload.Enabled = True
        Me.btnCancel.Enabled = False
        If cancelled Then
            Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_1")
        Else
            Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_2")
        End If
        Me.ProgressBar1.Value = 0
        'for localization purpose -V-
        Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5") + " "
        Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6") + " "
        Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3") + " "
        Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2") + " "
        Me.Label4.Text = ""
    End Sub
    Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
        Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3") + " " & Math.Round((length / 1024), 2) & " KB"
        Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5") + " " & Me.txtFileName.Text
        Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_3") + " " & Math.Round((position / 1024), 2) & " KB " + Main_Win.rm.GetString("Downloader_Label4_4") + " " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
        If speed = -1 Then
            Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2_2")
        Else
            Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2") + " " & Math.Round((speed / 1024), 2) & " KB/s"
        End If
        Me.ProgressBar1.Value = percent
        If percent = 100 Then
            Me.Close()
        End If
    End Sub
    Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        If Me.txtFileName.Text <> "" Then
            Me.SaveFileDialog1.InitialDirectory = dir
            Me.SaveFileDialog1.FileName = whereToSave
            If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.whereToSave = Me.SaveFileDialog1.FileName
                Me.SaveFileDialog1.FileName = ""
                Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6") + " " & Me.whereToSave
                Me.txtFileName.Enabled = False
                Me.btnDownload.Enabled = False
                Me.btnCancel.Enabled = True
                Me.BackgroundWorker1.RunWorkerAsync()
            End If
        Else
            MessageBox.Show(Main_Win.rm.GetString("Downloader_msgbox1"), Main_Win.rm.GetString("Downloader_msgbox1_1"), MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Try
            theRequest = WebRequest.Create(Me.txtFileName.Text)
            theResponse = theRequest.GetResponse
        Catch ex As Exception
            MessageBox.Show(Main_Win.rm.GetString("Downloader_msgbox2") & ControlChars.CrLf & _
                            Main_Win.rm.GetString("Downloader_msgbox2_2") & ControlChars.CrLf & _
                           Main_Win.rm.GetString("Downloader_msgbox2_3"), Main_Win.rm.GetString("Downloader_msgbox2_4"), MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Me.Invoke(cancelDelegate, True)
            Exit Sub
        End Try
        Dim length As Long = theResponse.ContentLength
        Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
        Me.Invoke(safedelegate, length, 0, 0, 0)
        Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
        Dim nRead As Integer
        Dim speedtimer As New Stopwatch
        Dim currentspeed As Double = -1
        Dim readings As Integer = 0
        Do
            If BackgroundWorker1.CancellationPending Then
                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 = (nRead * 100) / length
            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
                currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                speedtimer.Reset()
                readings = 0
            End If
        Loop
        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 Downloader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label1.Text = Main_Win.rm.GetString("Downloader_Label1")
        Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2")
        Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3")
        Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4")
        Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5")
        Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6")
        Me.btnDownload.Text = Main_Win.rm.GetString("Downloader_btnDownload")
        Me.btnCancel.Text = Main_Win.rm.GetString("Downloader_btnCancel")
        Me.Label4.Text = ""
        Me.btnDownload.ForeColor = Color.Black
        Me.btnCancel.ForeColor = Color.Black
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.BackgroundWorker1.CancelAsync()
    End Sub
End Class


and thgis is the request handler in the main form:

VB
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.Show()
        downloader.txtFileName.Text = e.Url.ToString
        downloader.whereToSave = e.SuggestedFileName
        downloader.dir = GetDownloadsPath()
        newItem.SubItems.Add(e.Url.ToString)
'the e.url.tostring can be http://www.someurl.com/download.php?file.zip
'but also http://www.codeproject.com/.../someproject.zip but both don't work
        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


and this is the getdownloadpath function(if necessarily)

VB
<DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function SHGetKnownFolderPath(ByRef id As Guid, flags As Integer, token As IntPtr, ByRef path As IntPtr) As Integer
    End Function
    Public Function GetDownloadsPath() As String
        Dim path__1 As String = Nothing
        If Environment.OSVersion.Version.Major >= 6 Then
            Dim pathPtr As IntPtr
            Dim hr As Integer = SHGetKnownFolderPath(FolderDownloads, 0, IntPtr.Zero, pathPtr)
            If hr = 0 Then
                path__1 = Marshal.PtrToStringUni(pathPtr)
                Marshal.FreeCoTaskMem(pathPtr)
                Return path__1
            End If
        End If
        path__1 = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal))
        path__1 = Path.Combine(path__1, "Downloads")
        Return path__1
    End Function
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900