Need suggestions on Response.BinaryWrite
am 02.01.2007 20:04:38 von mattridings
Hi gang,
Have a script that works fine. However, it's really cpu intensive and
I'm looking for suggestions on a) whether or not that's normal and if
so b)a better way of doing it.
Script is very simple, there is a folder of files with numeric names.
The real file name is stored in the database. User clicks on link that
executes script and provides database record id. Record is looked up,
file name is found, and browser is delivered the binary file via
Response.BinaryWrite. Pretty basic. However, when you run it if the
file is very large at all (even 1 meg) there is a significant delay
prior to giving the user the "save, run" prompt and the cpu on the
server jumps to 50-100% utilization and stays there for quite a while.
While I know that it basically has to read the file in to stream it out
it seems like that's quite a lot of cpu processing for such a simple
task, therefore I'm wondering if it's something in my script. I've
pasted that below.
The other related question is whether there is a way to manipulate the
content-disposition header for the filename and link the browser
directly to the file instead of having to stream it? In essence the
only thing I need is to a)link browser to file for download and
b)provide proper name for file. Any help much appreciated.
[Code]
' set the directory that contains the files here
strFileName = sClientFileFolderDirectory & "\" & Cstr(id)
Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
Set Bin = Sys.OpenTextFile( strFileName, 1, False )
If Sys.FileExists( strFileName ) Then
' Set the Filename to save as
Call Response.AddHeader( "Content-Disposition", "attachment;
filename=" & FileName )
' Make sure the browser downloads, instead of running it
Response.ContentType = "application/octet-stream"
' Send as a Binary Byte Stream
While Not Bin.AtEndOfStream
Response.BinaryWrite( ChrB( Asc( Bin.Read( 1 ) ) ) )
Wend
End If
Bin.Close : Set Bin = Nothing
Set Sys = Nothing
set oSession = Nothing
[Code]
Cheers,
-Matt
Re: Need suggestions on Response.BinaryWrite
am 03.01.2007 17:30:46 von Anthony Jones
wrote in message
news:1167764678.130127.238210@n51g2000cwc.googlegroups.com.. .
> Hi gang,
>
> Have a script that works fine. However, it's really cpu intensive and
> I'm looking for suggestions on a) whether or not that's normal and if
> so b)a better way of doing it.
>
> Script is very simple, there is a folder of files with numeric names.
> The real file name is stored in the database. User clicks on link that
> executes script and provides database record id. Record is looked up,
> file name is found, and browser is delivered the binary file via
> Response.BinaryWrite. Pretty basic. However, when you run it if the
> file is very large at all (even 1 meg) there is a significant delay
> prior to giving the user the "save, run" prompt and the cpu on the
> server jumps to 50-100% utilization and stays there for quite a while.
> While I know that it basically has to read the file in to stream it out
> it seems like that's quite a lot of cpu processing for such a simple
> task, therefore I'm wondering if it's something in my script. I've
> pasted that below.
>
> The other related question is whether there is a way to manipulate the
> content-disposition header for the filename and link the browser
> directly to the file instead of having to stream it? In essence the
> only thing I need is to a)link browser to file for download and
> b)provide proper name for file. Any help much appreciated.
>
> [Code]
> ' set the directory that contains the files here
> strFileName = sClientFileFolderDirectory & "\" & Cstr(id)
>
> Set Sys = Server.CreateObject( "Scripting.FileSystemObject" )
> Set Bin = Sys.OpenTextFile( strFileName, 1, False )
> If Sys.FileExists( strFileName ) Then
>
> ' Set the Filename to save as
> Call Response.AddHeader( "Content-Disposition", "attachment;
> filename=" & FileName )
>
> ' Make sure the browser downloads, instead of running it
> Response.ContentType = "application/octet-stream"
>
> ' Send as a Binary Byte Stream
> While Not Bin.AtEndOfStream
> Response.BinaryWrite( ChrB( Asc( Bin.Read( 1 ) ) ) )
> Wend
> End If
> Bin.Close : Set Bin = Nothing
> Set Sys = Nothing
> set oSession = Nothing
>
> [Code]
>
> Cheers,
>
Use this function instead:-
Sub SendFileToResponse(FilePath, FileName)
Const clChunkSize = 1048576 ' 1MB
Dim oStream, i
Response.Buffer = False
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", _
"attachment; Filename=" & FileName
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' Binary
oStream.Open
oStream.LoadFromFile FilePath
For i = 1 To oStream.Size \ clChunkSize
Response.BinaryWrite oStream.Read(clChunkSize)
Next
If (oStream.Size Mod clChunkSize) <> 0 Then
Response.BinaryWrite oStream.Read(oStream.Size Mod clChunkSize)
End If
oStream.Close
End Sub
Anthony.
> -Matt
>