Problem mit Video-Downloader

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von Myrax.

    Problem mit Video-Downloader

    Hi

    Ich habe ein kleines Programm geschrieben das Dateien downloaden kann. Nur mein Problem ist die gedownloadeten Dateien werden als "pure" Dateien dargestellt. Also als Dateien ohne Dateinamenserweiterung das Icon dieser Dateien ist einfach ein weißes Blatt mit einer Knickecke oben rechts.

    Meine Frage nun was muss ich an diesem Code ändern so das die Datei im MP3-Format gespeichert wird (mit dem Filter beim SaveFileDialog hab es schon versucht, hat nicht funktioniert)

    Der Code:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Net
    2. Public Class Form1
    3. Public Const Clickdown As Integer = &HA1
    4. Public Const FormCapture As Integer = &H2
    5. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Int32, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    6. Private Declare Function ReleaseCapture Lib "user32.dll" () As Int32
    7. Dim whereToSave As String
    8. Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
    9. Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
    10. Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    11. If (e.Button = Windows.Forms.MouseButtons.Left) Then
    12. ReleaseCapture()
    13. SendMessage(Handle.ToInt32, Clickdown, FormCapture, 0)
    14. End If
    15. End Sub
    16. Public Sub DownloadComplete(ByVal cancelled As Boolean)
    17. Me.txtFileName.Enabled = True
    18. Me.btn_download.Enabled = True
    19. If cancelled Then
    20. Me.btn_cancel.Enabled = False
    21. Me.lblstat.Text = "Status : " & "Abgebrochen"
    22. MessageBox.Show("Download wurde abgebrochen !", "Abgebrochen", MessageBoxButtons.OK, MessageBoxIcon.Information)
    23. Else
    24. Me.btn_cancel.Enabled = False
    25. Me.lblstat.Text = "Status : " & "Successfully downloaded"
    26. MessageBox.Show("Erfolgreich heruntergeladen !", "Fertig", MessageBoxButtons.OK, MessageBoxIcon.Information)
    27. End If
    28. Me.ProgressBar1.Value = 0
    29. End Sub
    30. Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
    31. Me.lblsize.Text = "Größe: " & Math.Round((length / 1024), 2) & " KB"
    32. Me.lbldownloading.Text = "Downloading: " & Me.txtFilename.Text
    33. Me.lblstat.Text = "Status: " & Math.Round((position / 1024), 2) & " KB von " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
    34. Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
    35. If speed = -1 Then
    36. Me.lblspd.Text = "Geschwindigkeit: " & "wird ausgerechnet..."
    37. Else
    38. Me.lblspd.Text = "Geschwindigkeit: " & Math.Round((speed / 1024), 2) & " KB/s"
    39. End If
    40. Me.ProgressBar1.Value = percent
    41. End Sub
    42. Private Sub btn_download_Click(sender As System.Object, e As System.EventArgs) Handles btn_download.Click
    43. If Me.txtFilename.Text <> "" AndAlso Me.txtFilename.Text.StartsWith("http://") Then
    44. Me.whereToSave = Me.loc.Text
    45. Me.SaveFileDialog1.FileName = ""
    46. Me.lblsloc.Text = "Speicherort: " & whereToSave
    47. Me.txtFilename.Enabled = False
    48. Me.btn_download.Enabled = False
    49. Me.btn_cancel.Enabled = True
    50. Me.loc.Enabled = False
    51. Me.brws.Enabled = False
    52. Me.BackgroundWorker1.RunWorkerAsync()
    53. Else
    54. MessageBox.Show("Dieser Link verweist auch kein gültiges Videoformat", "Warnung", MessageBoxButtons.OK, MessageBoxIcon.Warning)
    55. End If
    56. End Sub
    57. Private Sub brws_Click(sender As System.Object, e As System.EventArgs) Handles brws.Click
    58. Me.SaveFileDialog1.FileName = Me.txtFilename.Text.Split("/"c)(Me.txtFilename.Text.Split("/"c).Length - 1)
    59. Me.lblname.Text = "Name: " & Me.txtFilename.Text.Split("/"c)(Me.txtFilename.Text.Split("/"c).Length - 1)
    60. Me.SaveFileDialog1.ShowDialog()
    61. Me.loc.Text = Me.SaveFileDialog1.FileName
    62. End Sub
    63. Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    64. Me.btn_download.Enabled = False
    65. Dim theResponse As HttpWebResponse
    66. Dim theRequest As HttpWebRequest
    67. Try
    68. theRequest = WebRequest.Create(Me.txtFilename.Text)
    69. theResponse = theRequest.GetResponse
    70. Catch ex As Exception
    71. MessageBox.Show("Ein Fehler ist aufgetreten während sie die Datei(en) gedownloadet haben. Mögliche Ursachen:" & ControlChars.CrLf & _
    72. "1) Die Datei existiert n" & ControlChars.CrLf & _
    73. "2) Webserver-Fehler, versuchen sie es später nochmal", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    74. Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    75. Me.Invoke(cancelDelegate, True)
    76. Exit Sub
    77. End Try
    78. Dim length As Long = theResponse.ContentLength
    79. Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
    80. Me.Invoke(safedelegate, length, 0, 0, 0)
    81. Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
    82. Dim nRead As Integer
    83. Dim speedtimer As New Stopwatch
    84. Dim currentspeed As Double = -1
    85. Dim readings As Integer = 0
    86. Do
    87. If BackgroundWorker1.CancellationPending Then
    88. Exit Do
    89. End If
    90. speedtimer.Start()
    91. Dim readBytes(4095) As Byte
    92. Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
    93. nRead += bytesread
    94. Dim percent As Short = (nRead / length) * 100
    95. Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
    96. If bytesread = 0 Then Exit Do
    97. writeStream.Write(readBytes, 0, bytesread)
    98. speedtimer.Stop()
    99. readings += 1
    100. If readings >= 5 Then
    101. currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
    102. speedtimer.Reset()
    103. readings = 0
    104. End If
    105. Loop
    106. theResponse.GetResponseStream.Close()
    107. writeStream.Close()
    108. If Me.BackgroundWorker1.CancellationPending Then
    109. IO.File.Delete(Me.whereToSave)
    110. Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    111. Me.Invoke(cancelDelegate, True)
    112. Exit Sub
    113. End If
    114. Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    115. Me.Invoke(completeDelegate, False)
    116. End Sub
    117. End Class



    Möglicherweise wichtige angaben: "Me.loc" ist die Textbox mit dem Speicherort. "Me.sloc" ist der Label mit dem Inhalt von "Me.loc".
    @PhoenixBlaster
    Mal nebenbei. Für Downloadas würde ich einen WebClienten benutzen.

    VB.NET-Quellcode

    1. Option Strict On
    2. Public Class Form1
    3. Public WithEvents wc As New Net.WebClient()
    4. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    5. wc.DownloadFileAsync(New Uri("Adresse der Ressource"), "Pfad zum Speichern")
    6. End Sub
    7. Private Sub wc_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles wc.DownloadFileCompleted
    8. MessageBox.Show("Fertig")
    9. End Sub
    10. Private Sub wc_DownloadProgressChanged(sender As Object, e As Net.DownloadProgressChangedEventArgs) Handles wc.DownloadProgressChanged
    11. ProgressBar1.Value = e.ProgressPercentage
    12. End Sub
    13. End Class

    Mehr an Code ist es nicht. Keine Berechnungen, kein Backgroundworker, gar nichts ...
    Ich hab einen Filter mit dem geht es nicht wenn ich keinen Filter habe und schreibe "DerNameDerDatei.mp3" kann es trotzdem nicht geöffnet werden. Ich habe es an Windows MediaPlayer, VLC Player und QuickTime getestet
    @PhoenixBlaster: Wenn Du eine Datei ziehst solltest Du wissen, was es für eine ist.
    Was wird Dir denn angeboten, wenn Du sie im IE, FF oder so downloadest?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    @LaMiy: Genau. ;)
    @PhoenixBlaster:

    RodFromGermany schrieb:

    Was wird Dir denn angeboten, wenn Du sie im IE, FF oder so downloadest?
    Warum schreibst Du solch vollkommen irrelevante Information schon in Post #8 :?:
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!