5 Replies - 548 Views - Last Post: 15 June 2012 - 12:07 PM Rate Topic: -----

#1 Aki Maki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-June 12

I have some questions

Posted 15 June 2012 - 04:51 AM

Hello. I have some questions with the code uploaded above. As you see, it is an advanced file downloader. Now, my first question is:

1. How to add a filter (or automatic extension recognizer) in the screen of ... button? And, how to code the program to automatically add the extension of the given URL?

2. I want to code the Open File button in Form2 to open the downloaded file. I tried to use the downloading : label, but it just gave me some errors. What shall I do?

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: I have some questions

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: I have some questions

Posted 15 June 2012 - 05:54 AM

1.you can add filter to savefiledialog.
example:
SaveFileDialog1.Filter = "FLV files (*.flv)|*.flv|MP4 Files (*.mp4)|*.mp4"


Was This Post Helpful? 1
  • +
  • -

#3 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 292
  • Joined: 22-August 11

Re: I have some questions

Posted 15 June 2012 - 05:55 AM

Is this the code you're referring too?
Posted it cuz some people don't like to download files.
Imports System.Net
Public Class Form1
    Dim whereToSave 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.btn_download.Enabled = True

        If cancelled Then
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Cancelled"
            MessageBox.Show("Download Cancelled !", "Aborted", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Successfully downloaded"
            Form2.Show()
        End If

        Me.ProgressBar1.Value = 0
    End Sub

    Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.lblsize.Text = "Size : " & Math.Round((length / 1024), 2) & " KB"
        Me.lbldownloading.Text = "Downloading : " & Me.txtFileName.Text
        Me.lblstat.Text = "Status : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
        Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
        If speed = -1 Then
            Me.lblspd.Text = "Speed : " & "calculating..."
        Else
            Me.lblspd.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent
    End Sub

    Private Sub btn_download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_download.Click
        If Me.txtfilename.Text <> "" AndAlso Me.txtfilename.Text.StartsWith("http://") Then
            Me.whereToSave = Me.loc.Text
            Me.SaveFileDialog1.FileName = ""
            Me.lblsloc.Text = "Save to : " & whereToSave
            Me.txtfilename.Enabled = False
            Me.btn_download.Enabled = False
            Me.btn_cancel.Enabled = True
            Me.loc.Enabled = False
            Me.brws.Enabled = False
            Me.BackgroundWorker1.RunWorkerAsync()
        Else
            MessageBox.Show("This url is not valid", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub brws_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brws.Click
        Me.SaveFileDialog1.FileName = Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.lblname.Text = "Name : " & Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.SaveFileDialog1.ShowDialog()
        Me.loc.Text = Me.SaveFileDialog1.FileName
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.btn_download.Enabled = False
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Try
            theRequest = WebRequest.Create(Me.txtfilename.Text)
            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)
            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 Double = (nRead / length) * 100
            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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btn_cancel.Enabled = False
    End Sub

    Private Sub lbldownloading_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbldownloading.Click

    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: I have some questions

Posted 15 June 2012 - 05:57 AM

I tried to use the downloading : label, but it just gave me some errors.
What error?
Was This Post Helpful? 0
  • +
  • -

#5 Aki Maki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-June 12

Re: I have some questions

Posted 15 June 2012 - 10:50 AM

View Postsela007, on 15 June 2012 - 12:54 PM, said:

1.you can add filter to savefiledialog.
example:
SaveFileDialog1.Filter = "FLV files (*.flv)|*.flv|MP4 Files (*.mp4)|*.mp4"



Thanks a lot! That's what I needed for my programs!

View Posttorind_2000, on 15 June 2012 - 12:55 PM, said:

Is this the code you're referring too?
Posted it cuz some people don't like to download files.
Imports System.Net
Public Class Form1
    Dim whereToSave 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.btn_download.Enabled = True

        If cancelled Then
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Cancelled"
            MessageBox.Show("Download Cancelled !", "Aborted", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Successfully downloaded"
            Form2.Show()
        End If

        Me.ProgressBar1.Value = 0
    End Sub

    Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.lblsize.Text = "Size : " & Math.Round((length / 1024), 2) & " KB"
        Me.lbldownloading.Text = "Downloading : " & Me.txtFileName.Text
        Me.lblstat.Text = "Status : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
        Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
        If speed = -1 Then
            Me.lblspd.Text = "Speed : " & "calculating..."
        Else
            Me.lblspd.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent
    End Sub

    Private Sub btn_download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_download.Click
        If Me.txtfilename.Text <> "" AndAlso Me.txtfilename.Text.StartsWith("http://") Then
            Me.whereToSave = Me.loc.Text
            Me.SaveFileDialog1.FileName = ""
            Me.lblsloc.Text = "Save to : " & whereToSave
            Me.txtfilename.Enabled = False
            Me.btn_download.Enabled = False
            Me.btn_cancel.Enabled = True
            Me.loc.Enabled = False
            Me.brws.Enabled = False
            Me.BackgroundWorker1.RunWorkerAsync()
        Else
            MessageBox.Show("This url is not valid", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub brws_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brws.Click
        Me.SaveFileDialog1.FileName = Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.lblname.Text = "Name : " & Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.SaveFileDialog1.ShowDialog()
        Me.loc.Text = Me.SaveFileDialog1.FileName
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.btn_download.Enabled = False
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Try
            theRequest = WebRequest.Create(Me.txtfilename.Text)
            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)
            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 Double = (nRead / length) * 100
            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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btn_cancel.Enabled = False
    End Sub

    Private Sub lbldownloading_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbldownloading.Click

    End Sub
End Class



Yea! That's it! Nice program, ha? It's open source.
Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 840
  • Joined: 21-December 11

Re: I have some questions

Posted 15 June 2012 - 12:07 PM

View PostAki Maki, on 15 June 2012 - 10:50 AM, said:

Yea! That's it! Nice program, ha? It's open source.

So this is some kind of youtube downloader? Is this working? I've tried but there is error in BackgroundWorker1_DoWork.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1