Question Help Downloading Videos...

Hassan3D

New member
Joined
Aug 25, 2008
Messages
1
Programming Experience
3-5
I have created a simple downloader with vb.net, that downloads youtube videos. The downloading starts very good but it displays the following error when I downloaded 20.48 MB

Arithmetic operation resulted in an overflow.

The code is written below:

VB.NET:
[COLOR="Blue"]Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Try
            Dim theResponse As HttpWebResponse
            Dim theRequest As HttpWebRequest
            Try 'Checks if the file exist
                If filters = "MP4 File(*.mp4)|*.mp4" Then

                    theRequest = WebRequest.Create(Svideoaddress2)
                Else
                    theRequest = WebRequest.Create(Svideoaddress1)
                End If
                theResponse = theRequest.GetResponse
            Catch ex As Exception
                MsgBox(ex.Message)
                If ex.Message.Contains("Unable to read data from the transport connection:") Then
                    MsgBox("You are disconnected from internet. Please connect to internet and then try again.", MsgBoxStyle.Information, "Internet Unavailable")
                    Me.BackgroundWorker1.CancelAsync()
                Else
                    MessageBox.Show("Sorry high quality is not available for this video. You can download it in low quality.", "High Quality Video Unavailable")
                    lop = "ABC"
                    'addHighstate()
                    Me.BackgroundWorker1.CancelAsync()
                End If

                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)

                Me.Invoke(cancelDelegate, True)

                Exit Sub
            End Try
            Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)

            Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
            Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate

            Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)


            'Replacement for Stream.Position (webResponse stream doesn't support seek)
            Dim nRead As Integer

            'To calculate the download speed
            Dim speedtimer As New Stopwatch
            Dim currentspeed As Double = -1
            Dim readings As Integer = 0
            Try
                Do

                    If BackgroundWorker1.CancellationPending Then 'If user abort download
                        Exit Do
                    End If

                    speedtimer.Start()

                    [COLOR="Red"]Dim readBytes(4096) 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 'For increase precision, the speed it's calculated only every five cicles
                        currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                        speedtimer.Reset()
                        readings = 0
                    End If



                Loop

                'Close the streams
                theResponse.GetResponseStream.Close()
                writeStream.Close()[/COLOR]
            Catch ex As Exception
                If ex.Message.Contains("Unable to read data from the transport connection") Then
                    MsgBox("You are disconnected from internet. Please connect to internet and then try again.", MsgBoxStyle.Information, "Internet Unavailable")
                    Me.BackgroundWorker1.CancelAsync()

                Else
                    MsgBox("An unexpected error occured while downloading: " & vbCrLf & ex.Message, MsgBoxStyle.Information, "Down2PC - An unexpected error")
                    Me.BackgroundWorker1.CancelAsync()
                End If
            End Try

            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)

        Catch ex As Exception
            If ex.Message.Contains("The given path's format is not supported") Then
                MsgBox("The given output location for the video is invalid. Your video filename must not contain   /,\,*,<,>,|,? and :   
Please correct it and then try again.", MsgBoxStyle.Information, "Invalid Path")
                Me.BackgroundWorker1.CancelAsync()
                Me.Height = 311
            ElseIf ex.Message.Contains("Arithmetic") Then
                Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)

                Me.Invoke(completeDelegate, False)

                'MsgBox("Error Downloading Video...Video download will not exit", MsgBoxStyle.Information, "Error!")
            Else
                MsgBox("Error Occurred while downloading: " & vbCrLf & ex.Message, MsgBoxStyle.Information, "Error!")

            End If
           
        End Try
        'Creating the request and getting the response

    End Sub[/COLOR]

Please make correction to this code, OR tell me the problem. Please
I'll be gratful to you.

Hassan
Pakistan.
 
Last edited:

bader1

New member
Joined
Nov 2, 2008
Messages
1
Programming Experience
1-3
This should work...

Change this
Dim percent As Short = (nRead * 100) / length

to this
Dim percent As Short = (nRead / length) *100

This should work
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
(you'll get an overflow when you have downloaded 2048mb instead of 20.48)

Use a data type with more storage, to store your nRead (i.e. Long, not Int32)
 
Top Bottom