Search code examples
vb.netdownloadhttpwebrequesthttpwebresponse

Resume download with stream.addrange


I want to resume a download by append the file and the problem is that it make the file become bigger for example File A size is 98000kb and when the program download File A, the internet problem or user abort the download and the download halt at 2000kb. The user then proceeds with resume download which it resume the download but, File A size has become 100000 kb. is there a way to solve this problem? Here's my code:

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork        
    Dim url As String = e.Argument
    'Creating the request and getting the response
    Dim strFileName
    Dim a As String
    Dim b() As String
    files = url.Split(CChar(","))
    For Each a In files
        b = a.Split("/")
        strFileName = b(b.Length - 1)
        Me.whereToSave = "C:\Temp\" & strFileName
    Next

    Dim sDestinationPath As String = me.whereToSave 'file destination

    Dim iFileSize As Long = 0
    Dim iBufferSize As Integer = 1024
    iBufferSize *= 1000
    Dim iExistLen As Long = 0
    Dim saveFileStream As System.IO.FileStream = Nothing

    Dim hwRq As System.Net.HttpWebRequest
    Dim hwRes As System.Net.HttpWebResponse
    hwRq = DirectCast(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
    hwRq.AddRange(CInt(iExistLen))
    Dim smRespStream As System.IO.Stream
    hwRes = DirectCast(hwRq.GetResponse(), System.Net.HttpWebResponse)
    smRespStream = hwRes.GetResponseStream()

    iFileSize = hwRes.ContentLength
    If System.IO.File.Exists(sDestinationPath) Then
        Dim fINfo As New System.IO.FileInfo(sDestinationPath)
        iExistLen = fINfo.Length
    End If

    If iExistLen = iFileSize Then
        MsgBox("exists")
        saveFileStream = New System.IO.FileStream(sDestinationPath, System.IO.FileMode.Open, IO.FileAccess.Write, System.IO.FileShare.Read)
    ElseIf iExistLen > 0 And iExistLen <= iFileSize Then
        MsgBox("Append")
        saveFileStream = New System.IO.FileStream(sDestinationPath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
    Else
        MsgBox("Create")
        saveFileStream = New System.IO.FileStream(sDestinationPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
    End If
    Dim iByteSize As Integer
    Dim downBuffer As Byte() = New Byte(iBufferSize - 1) {}
    Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
    iByteSize = (smRespStream.Read(downBuffer, 0, downBuffer.Length))
    While (iByteSize > 0)
        saveFileStream.Write(downBuffer, 0, iByteSize)
        iByteSize = (smRespStream.Read(downBuffer, 0, downBuffer.Length))
    End While

Solution

  • You are setting hwRq.AddRange(CInt(iExistLen)) before you check if the file already exists and setting it's length.

    Move this

    If System.IO.File.Exists(sDestinationPath) Then
            Dim fINfo As New System.IO.FileInfo(sDestinationPath)
            iExistLen = fINfo.Length
        End If
    

    Above the definition of hwRq.AddRange(CInt(iExistLen))