Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Upload File Progress bar

Upload File Progress bar

Scheduled Pinned Locked Moved ASP.NET
com
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcostello
    wrote on last edited by
    #1

    Hi I have been trying to build a progress bar for uploading files. Using the code at http://www.codeproject.com/useritems/File\_Upload\_Progress\_Bar.asp as a template, I built the following code. This seems to work perfectly until you look at the uploaded file which is missing some data. E.g. a file that has a File Size of 81,392 and a Size on Disk of 81,920, when uploaded has a file size of 77,824 and a size on disk of 77,824. Any ideas gratefully accepted or I'll have no hair left soon..... *******************Class****************************** Public Class UploadThread Private _bytesRead As Long Private _tID As Thread Private _filename As String Private _stream As Stream Private _uploaded As Boolean Private _exception As Exception #Region "Constructors" Public Sub New() End Sub 'New #End Region Public Property BytesRead() As Long ' How many bytes has been uploaded. Get Return _bytesRead End Get Set(ByVal Value As Long) _bytesRead = Value End Set End Property Public Property Length() As Long ' Length of data to upload Get Return _stream.Length End Get Set(ByVal Value As Long) End Set End Property Public Property Uploaded() As Boolean ' Determine upload was finished Get Return _uploaded End Get Set(ByVal Value As Boolean) End Set End Property Public Property Percent() As Integer ' How many percent has been uploaded Get Return ((BytesRead * 100) / Length) End Get Set(ByVal Value As Integer) End Set End Property Public Property Exception() As Exception ' Exception object, is there was an exception, otherwise: null. Get Return _exception End Get Set(ByVal Value As Exception) End Set End Property '/// '/// Class contructor. '/// Initialize fields and create uploading thread. '/// '/// File's name, with the path, to save '/// Upload data's stream (ex. .PostedFile.InputStream) Public Function FileUploadThread(ByVal filename As Stri

    M 1 Reply Last reply
    0
    • M mcostello

      Hi I have been trying to build a progress bar for uploading files. Using the code at http://www.codeproject.com/useritems/File\_Upload\_Progress\_Bar.asp as a template, I built the following code. This seems to work perfectly until you look at the uploaded file which is missing some data. E.g. a file that has a File Size of 81,392 and a Size on Disk of 81,920, when uploaded has a file size of 77,824 and a size on disk of 77,824. Any ideas gratefully accepted or I'll have no hair left soon..... *******************Class****************************** Public Class UploadThread Private _bytesRead As Long Private _tID As Thread Private _filename As String Private _stream As Stream Private _uploaded As Boolean Private _exception As Exception #Region "Constructors" Public Sub New() End Sub 'New #End Region Public Property BytesRead() As Long ' How many bytes has been uploaded. Get Return _bytesRead End Get Set(ByVal Value As Long) _bytesRead = Value End Set End Property Public Property Length() As Long ' Length of data to upload Get Return _stream.Length End Get Set(ByVal Value As Long) End Set End Property Public Property Uploaded() As Boolean ' Determine upload was finished Get Return _uploaded End Get Set(ByVal Value As Boolean) End Set End Property Public Property Percent() As Integer ' How many percent has been uploaded Get Return ((BytesRead * 100) / Length) End Get Set(ByVal Value As Integer) End Set End Property Public Property Exception() As Exception ' Exception object, is there was an exception, otherwise: null. Get Return _exception End Get Set(ByVal Value As Exception) End Set End Property '/// '/// Class contructor. '/// Initialize fields and create uploading thread. '/// '/// File's name, with the path, to save '/// Upload data's stream (ex. .PostedFile.InputStream) Public Function FileUploadThread(ByVal filename As Stri

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi Mark, When the size of the uploaded file is not correct, I think that there might be a problem with the way you use to read data from the input stream and write out to a file. Here, I just rewrite the uploadThread method to make sure that the data doesn't get lost as well as there's no additional data.

      Private Sub uploadThread()
      Dim bytesRemaining As Integer = _stream.Length
      Dim bytesToRead As Integer = Math.Min(1024, bytesRemaining)
      Try
      Dim fs As FileStream = New FileStream(_filename, FileMode.Create)
      While (bytesToRead > 0)
      Dim buffer(bytesToRead) As Byte

              \_stream.Read(buffer, 0, buffer.GetUpperBound(0))
                           
              fs.Write(buffer, 0, buffer.GetUpperBound(0))
              fs.Flush()
                        
              'Update display data.
              \_bytesRead += bytesToRead
                         
              'Update reading parameters
              bytesRemaining = bytesRemaining - bytesToRead
              bytesToRead = Math.Min(1024, bytesRemaining)
                         
              System.Threading.Thread.Sleep(100)
          End While
          fs.Close()
      Catch ex As Exception
          \_exception = ex
      End Try
      \_uploaded = True
      

      End Sub

      M 1 Reply Last reply
      0
      • M minhpc_bk

        Hi Mark, When the size of the uploaded file is not correct, I think that there might be a problem with the way you use to read data from the input stream and write out to a file. Here, I just rewrite the uploadThread method to make sure that the data doesn't get lost as well as there's no additional data.

        Private Sub uploadThread()
        Dim bytesRemaining As Integer = _stream.Length
        Dim bytesToRead As Integer = Math.Min(1024, bytesRemaining)
        Try
        Dim fs As FileStream = New FileStream(_filename, FileMode.Create)
        While (bytesToRead > 0)
        Dim buffer(bytesToRead) As Byte

                \_stream.Read(buffer, 0, buffer.GetUpperBound(0))
                             
                fs.Write(buffer, 0, buffer.GetUpperBound(0))
                fs.Flush()
                          
                'Update display data.
                \_bytesRead += bytesToRead
                           
                'Update reading parameters
                bytesRemaining = bytesRemaining - bytesToRead
                bytesToRead = Math.Min(1024, bytesRemaining)
                           
                System.Threading.Thread.Sleep(100)
            End While
            fs.Close()
        Catch ex As Exception
            \_exception = ex
        End Try
        \_uploaded = True
        

        End Sub

        M Offline
        M Offline
        mcostello
        wrote on last edited by
        #3

        Thanks a million - works perfectly. Mark Costello

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups