本文介绍了webbrowser的文件下载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我已经制作了一个内置下载器的网络(套件)浏览器,但当我试图下载像这样的文件时: br />


www.some-url.com/download.php?file=some_filenam.txt它没有下载文件但下载了页面download.php并保存它作为some_filename.txt



我该如何解决这个问题



代码我用来下载:

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:

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




Bart de Lange

推荐答案


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





和thgis是主表单中的请求处理程序:





and thgis is the request handler in the main form:

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





这是getdownloadpath函数(如果必要的话)





and this is the getdownloadpath function(if necessarily)

<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


这篇关于webbrowser的文件下载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:48