Results 1 to 3 of 3

Thread: Whats the best download method for vb?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2013
    Posts
    12

    Wink Whats the best download method for vb?

    I am trying to make a download client with ftp and im still trying to find out whats the best i have but im in stuck because i followed a guide but it only show how much its have downloaded in bytes, how can i change it to gb? and how can i add a download speed?

    Code:
    Imports System.Net
    Public Class MainForm
    
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
    
    Private Sub btnBrowseSave_Click(sender As Object, e As EventArgs) Handles btnBrowseSave.Click
        Dim newFolder As New FolderBrowserDialog
        If newFolder.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtSavePath.Text = newFolder.SelectedPath
        End If
    End Sub
    
    Private Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
        bWorker.RunWorkerAsync()
    End Sub
    
    Private Sub bWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bWorker.DoWork
        Dim buffer(1023) As Byte
        Dim bytesIn As Integer
        Dim totalBytesIn As Integer
        Dim output As IO.Stream
        Dim flLength As Integer
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = Net.WebRequestMethods.Ftp.GetFileSize
            flLength = CInt(FTPRequest.GetResponse.ContentLength)
            lblFileSize.Text = flLength & " bytes"
        Catch ex As Exception
    
        End Try
        Try
            Dim FTPRequest As FtpWebRequest = DirectCast(WebRequest.Create(txtFilePath.Text), FtpWebRequest)
            FTPRequest.Credentials = New NetworkCredential(txtFTPUsername.Text, txtFTPPassword.Text)
            FTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
            Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
            Dim OutputFilePath As String = txtSavePath.Text & "\" & IO.Path.GetFileName(txtFilePath.Text)
            output = System.IO.File.Create(OutputFilePath)
            bytesIn = 1
            Do Until bytesIn < 1
                bytesIn = stream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    output.Write(buffer, 0, bytesIn)
                    totalBytesIn += bytesIn
                    lblDownloadedBytes.Text = totalBytesIn.ToString & " bytes"
                    If flLength > 0 Then
                        Dim perc As Integer = (totalBytesIn / flLength) * 100
                        bWorker.ReportProgress(perc)
                    End If
                End If
            Loop
            output.Close()
            stream.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    
    Private Sub bWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bWorker.ProgressChanged
        pBar.Value = e.ProgressPercentage
        lblPercent.Text = e.ProgressPercentage.ToString & "%"
    End Sub
    
    Private Sub bWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bWorker.RunWorkerCompleted
        MsgBox("Download Complete!")
    End Sub
    
    
    End Class

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Whats the best download method for vb?

    Control.CheckForIllegalCrossThreadCalls = False

    dont ever use this, it makes it worse you are using the BGW that handles invoking for us.
    My Github - 1d3nt

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Whats the best download method for vb?

    how can i change it to gb
    GB=Bytes / (1024^3)

    and how can i add a download speed?
    Speed=TotalBytesReceived/SecondsElapsed

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width