Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 2nd, 2007, 07:15 PM
mattridings@gmail.com
Guest
 
Posts: n/a
Default Need suggestions on Response.BinaryWrite

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

  #2  
Old January 3rd, 2007, 04:45 PM
Anthony Jones
Guest
 
Posts: n/a
Default Re: Need suggestions on Response.BinaryWrite


<mattridings@gmail.comwrote in message
news:1167764678.130127.238210@n51g2000cwc.googlegr oups.com...
Quote:
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.

Quote:
-Matt
>

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles