Solved

Upload progress bar

Posted on 2004-03-23
1
984 Views
Last Modified: 2012-06-22
We have developed an application using VB.Net. Application is uploading a file with the help of Webclient Upload. We need to show upload progress using a progress bar. Please give us some ideas on how this can be implemented?
0
Comment
Question by:zakali
1 Comment
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 10657335
You cannot have the progress bar during upload because you don't know the size of downloaded portion
Yoiu have 2 choices
1.SHow infinite process - just to let user know that something is going on

For example update progress bar on a timer (you have to do it in a different thread)


2.Download block and then update progress bar. Here is an example for copy file. You will get the idea
'add Button1 and ProgressBar1

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'if file exists it will overwrite it
        Me.FileCopyProgress("F:\Handheld\Friend\Data\HHPalm\CremosaBackEnd.mdb", "c:\a\cbe.mdb)
        MsgBox("Success")
    End Sub

    Private Sub FileCopyProgress(ByVal Filelocation As String, ByVal CopyToDestination As String)
        Dim ReturnValue As Boolean
        Dim TaskProgress As Integer 'contains the total value that this task takes in the progress bar
        Dim FileSize As Integer ' contains the size of the file wanting to be copied in bytes as integer
        Dim FileStreamCopyFrom As FileStream
        Dim FileStreamCopyTo As FileStream
        Dim loops As Integer
        Dim i As Integer
        Dim ByteSize As Integer
        Dim ExpectedLoops As Integer
        Dim IsWholeNumber As Integer

        IsWholeNumber = 1
        TaskProgress = ProgressBar1.Maximum - ProgressBar1.Minimum 'tells us how many 'bits' of the status bar this task takes up
        ByteSize = TaskProgress
        FileStreamCopyFrom = File.Open(Filelocation, FileMode.Open, FileAccess.Read)
        FileStreamCopyTo = File.Open(CopyToDestination, FileMode.OpenOrCreate, FileAccess.Write)
        FileSize = FileStreamCopyFrom.Length

        'We now want to find a number that can divide the total filesize into a whole number (leaving no reamainder)
        'the best way to do this is to use the TaskProgress value and work down to find this number
        Do
            IsWholeNumber = FileSize Mod (ByteSize + 1)
            If Not IsWholeNumber = 0 Then
                ByteSize = ByteSize - 1
            End If
        Loop While Not IsWholeNumber = 0

        Dim MyByte(ByteSize) As Byte 'declare MyByte as byte array using our new value

        ExpectedLoops = System.Math.Floor(FileSize / (ByteSize + 1)) 'Stores how many loops we expect to see by rounding down the result of div.

        If Not FileSize Mod (ByteSize + 1) = 0 Then
            ExpectedLoops = ExpectedLoops + 1
        End If

        i = FileStreamCopyFrom.Read(MyByte, 0, ByteSize + 1) '1024reads data in 1kb at a time to b2

        loops = 0  'set loops = 0 as we have not looped yet.
        Do
            loops += 1
            FileStreamCopyTo.Write(MyByte, 0, ByteSize + 1)
            i = FileStreamCopyFrom.Read(MyByte, 0, ByteSize + 1)
            'increase the progress bar
            ProgressBar1.Value = ProgressBar1.Minimum + ((TaskProgress / ExpectedLoops) * loops)
        Loop Until i = 0
        FileStreamCopyFrom.Close() 'close the filestreams
        FileStreamCopyTo.Close()   'close the filestreams
    End Sub

End Class

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question