Upload File Progress bar
-
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
-
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
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
-
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