I have the following code snippet to send a file stored as an image data
type from sql server:
Protected Sub StreamFile(ByVa l fileItem As MyFile)
Dim offset As Integer
Dim buffer As Integer = 8192
Dim textPtr As System.Data.Sql Types.SqlBinary =
MyFactory.GetFi lePtr(fileItem. ID, FileScale.Origi nal)
Response.Clear( )
Response.Buffer = False
Response.Conten tType = "applicatio n/octet-stream"
'should I set this???
'Response.AddHe ader("Content-Length", fileItem.ByteCo unt.ToString())
Response.AddHea der("Content-Disposition", "attachment ; filename=" +
fileItem.Name)
Response.Flush( )
Dim size As Integer
Do
If offset + buffer > fileItem.ByteCo unt Then
size = fileItem.ByteCo unt - offset
Else
size = buffer
End If
Response.Binary Write(MyFactory .GetFileChunk(t extPtr, offset,
fileItem.ByteCo unt))
Response.Flush( )
offset += size
Loop Until offset >= fileItem.ByteCo unt
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
type from sql server:
Protected Sub StreamFile(ByVa l fileItem As MyFile)
Dim offset As Integer
Dim buffer As Integer = 8192
Dim textPtr As System.Data.Sql Types.SqlBinary =
MyFactory.GetFi lePtr(fileItem. ID, FileScale.Origi nal)
Response.Clear( )
Response.Buffer = False
Response.Conten tType = "applicatio n/octet-stream"
'should I set this???
'Response.AddHe ader("Content-Length", fileItem.ByteCo unt.ToString())
Response.AddHea der("Content-Disposition", "attachment ; filename=" +
fileItem.Name)
Response.Flush( )
Dim size As Integer
Do
If offset + buffer > fileItem.ByteCo unt Then
size = fileItem.ByteCo unt - offset
Else
size = buffer
End If
Response.Binary Write(MyFactory .GetFileChunk(t extPtr, offset,
fileItem.ByteCo unt))
Response.Flush( )
offset += size
Loop Until offset >= fileItem.ByteCo unt
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