0

I'm building an website that allows users to upload/download/delete/manage files in a database.

For clarification, the web application uploads files to a database, NOT the file system. This is because of server constraints and I have no control over it.

I use a filestream to convert the file into a blob and then stick it in the database. What I'd like to know is:

Is it possible to get the progress of a filestream for large files? See how much has been streamed so far or set a timer to update that value?

My code is as follows:

Dim fs As Stream = upload1.PostedFile.InputStream
Dim br As New BinaryReader(fs)
Dim bytes as Byte() = br.ReadBytes(fs.Length)
Dim length As Integer = fs.Length

Then I add "bytes" as a parameter to my Stored Procedure and run the query. What could I add to this to maybe get the status of the filestream?

I hope this is clear, if not I can clarify.

Thanks!

Blunderfest
  • 1,854
  • 1
  • 28
  • 46
  • You should not allocate a big byte array, but instead loop using a fixed size byte array (preferrably with a size < 85000). This way you will get an implicit progress, plus you won't kill the large object heap, check this out: http://stackoverflow.com/questions/8951836/why-large-object-heap-and-why-do-we-care – Simon Mourier Apr 19 '13 at 17:40
  • I understand the difference and the link you provided was a good read, but I'd have no idea of how to translate the above to a fixed size byte array. Especially as some of the files I will be uploading will be very large (>50mg). – Blunderfest Apr 19 '13 at 17:51

1 Answers1

0

You could copy one stream to another using a code like this and insert your progress code in the middle:

Public Shared Sub Copy(ByVal source As Stream, ByVal destination As Stream, ByVal bufferSize As Integer)
    Dim num As Integer
    Dim buffer As Byte() = New Byte(bufferSize  - 1) {}
    Do While (num = source.Read(buffer, 0, buffer.Length) <> 0)
        destination.Write(buffer, 0, num)
        ' insert progress code here
    Loop
End Sub
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • I'm trying to understand the different parts of what this would be doing. The source would be the original file, while the destination would be the copy? And the buffersize? Is it not inefficient to run the stream twice for a copy? I am definitely new to working with filestream, so please forgive my ignorance. – Blunderfest Apr 19 '13 at 18:26
  • 1
    The source would be upload1.PostedFile.InputStream, the destination would be your target stream (database or other), and the bufferSize could be set to something < 85000, so maybe 65536 to make it round. – Simon Mourier Apr 20 '13 at 06:10