Sending Large File

P

PJ

I have the following code snippet to send a file stored as an image data
type from sql server:

Protected Sub StreamFile(ByVal fileItem As MyFile)
Dim offset As Integer
Dim buffer As Integer = 8192
Dim textPtr As System.Data.SqlTypes.SqlBinary =
MyFactory.GetFilePtr(fileItem.ID, FileScale.Original)

Response.Clear()
Response.Buffer = False
Response.ContentType = "application/octet-stream"
'should I set this???
'Response.AddHeader("Content-Length", fileItem.ByteCount.ToString())
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fileItem.Name)
Response.Flush()

Dim size As Integer
Do
If offset + buffer > fileItem.ByteCount Then
size = fileItem.ByteCount - offset
Else
size = buffer
End If
Response.BinaryWrite(MyFactory.GetFileChunk(textPtr, offset,
fileItem.ByteCount))
Response.Flush()
offset += size
Loop Until offset >= fileItem.ByteCount
End Sub

This seems to be working, but I would like to make sure I am doing
everything as effeciently as possible. My understanding is that the
..Flush() method will send the current contents of the response to the
client. My understanding of the internals of the actual HTTP response(s) is
a bit weak. Is my loop creating sending a new response during each
iteration? If this is the case do the headers need to be set again or does
it simply use the current settings for the headers? Should I set the
"Content-Length" header? If so, should I set it's value to that of the
total byte count of the file or to the size of each flushed response? And
in general...is there a better way to do this?

TIA~ PJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top