Download Module

Status
Niet open voor verdere reacties.

denzil

Gebruiker
Lid geworden
18 mei 2010
Berichten
5
Hey,
Ik heb laatst een applicatie geschreven waardoor het mogenlijk was om bepaalde bestanden binnen te halen en automatisch te laten verwijderen indien aangegeven op de server.
Welliswaar heeft het problemen met downloaden boven de circa 2.2GB

Ik gebruikte eerst Int32 voor het calculeren van het bestand. En volgens de error die ik ontving zou ik boven zijn limieten zitten.
Nu had ik dit omgezet naar String.

Welliswaar de calculatie (voor het weergeven van de voortgang) is een Long.

Naar mijn weten zou Long een grotere waarde aan moeten kunnen dan 2.2GB

Zie hier:

Code:
#Region "Header"

'2006 kleinma MSMVP
'www.vbforums.com

#End Region 'Header

Imports System.IO
Imports System.Net

Public Class WebFileDownloader

    #Region "Fields"

    Public mCurrentFile As String = String.Empty

    #End Region 'Fields

    #Region "Events"

    Public Event AmountDownloadedChanged(ByVal iNewProgress As Long)

    Public Event FileDownloadComplete()

    Public Event FileDownloadFailed(ByVal ex As Exception)

    Public Event FileDownloadSizeObtained(ByVal iFileSize As Long)

    #End Region 'Events

    #Region "Properties"

    Public ReadOnly Property CurrentFile() As String
        Get
            Return mCurrentFile
        End Get
    End Property

    #End Region 'Properties

    #Region "Methods"

    Public Shared Function FormatFileSize(ByVal Size As Long) As String
        Try
            Dim KB As Integer = 1024
            Dim MB As Integer = KB * KB
            ' Return size of file in kilobytes.
            If Size < KB Then
                Return (Size.ToString("D") & " bytes")
            Else
                Select Case Size / KB
                    Case Is < 1000
                        Return (Size / KB).ToString("N") & "KB"
                    Case Is < 1000000
                        Return (Size / MB).ToString("N") & "MB"
                    Case Is < 10000000
                        Return (Size / MB / KB).ToString("N") & "GB"
                End Select
            End If
        Catch ex As Exception
            Return Size.ToString
        End Try
        End
    End Function

    Public Function DownloadFile(ByVal URL As String, ByVal Location As String) As Boolean
        Try
            Dim WC As New WebClient
            WC.DownloadFile(URL, Location)
            Console.WriteLine(Location)
            RaiseEvent FileDownloadComplete()
            Return True
        Catch ex As Exception
            RaiseEvent FileDownloadFailed(ex)
            Return False
        End Try
    End Function

    Public Function DownloadFileWithProgress(ByVal URL As String, ByVal Location As String) As Boolean
        Dim FS As FileStream
        Try
            Dim wRemote As WebRequest
            Dim bBuffer As Byte()
            ReDim bBuffer(256)
            Dim iBytesRead As Double
            Dim iTotalBytesRead As Integer

            FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
            wRemote = WebRequest.Create(URL)
            Dim myWebResponse As WebResponse = wRemote.GetResponse
            RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
            Dim sChunks As Stream = myWebResponse.GetResponseStream
            Do
                iBytesRead = sChunks.Read(bBuffer, 0, 256)
                FS.Write(bBuffer, 0, iBytesRead)
                iTotalBytesRead += iBytesRead
                If myWebResponse.ContentLength < iTotalBytesRead Then
                    RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
                Else
                    RaiseEvent AmountDownloadedChanged(iTotalBytesRead)
                End If
            Loop While Not iBytesRead = 0
            sChunks.Close()
            FS.Close()
            RaiseEvent FileDownloadComplete()
            Return True
        Catch ex As Exception
            FS = Nothing
            If Not (FS Is Nothing) Then
                FS.Close()
                FS = Nothing
            End If
            RaiseEvent FileDownloadFailed(ex)
            Return False
        End Try
    End Function

    #End Region 'Methods

End Class
 
Status
Niet open voor verdere reacties.
Steun Ons

Nieuwste berichten

Terug
Bovenaan Onderaan