473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jan 2 '07 #1
1 4184

<ma*********@gmail.comwrote in message
news:11**********************@n51g2000cwc.googlegr oups.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

Jan 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zam | last post by:
Hello World, Under Windows 2003 Server. IIS6. The following code working fine for small files, and for files with size about few megabytes. If I am trying to send HUGE file -- about 700...
0
by: B_O_B | last post by:
I am using Response.BinaryWrite to display JPEG's to the browser. The problem is that in IE, one particular JPEG is loading very slow. Normal JPEG's take a few seconds, but this one sometimes...
1
by: unotin | last post by:
I have an application in ASP that exports to Word using the Response.ContentType method. The application references another ASP page through the img tag that uses a Response.BinaryWrite (of an...
3
by: spmm# | last post by:
Hi! My ASP.NET page gets a pdf-file from a SQLServer database and writes it in a browserwindow, using Response.BinaryWrite. It basically looks like this: public class WebForm1 :...
1
by: Charlie | last post by:
Hi: I'm uploading documents into a SQL Server Image field and using Response.BinaryWrite() to download or view them in the browser. Some doc types like Adobe Illustrator and Photoshop files...
1
by: Sridhar | last post by:
Hi, I am trying to display a pdf file on the web. It is working fine but the Page_Load method is running twice before displaying the page. Here is the code that I am using Private Sub...
2
by: ATS | last post by:
HOWTO Override ASP's Response output for 2003/IIS. Please help, I want to have an ASP page write back EVERYTHING to the ASP Response. That includes HTTP Headers. I want my ASP to look...
1
by: ChristopherABurns | last post by:
I have a very simple code snippet here: private void Page_Load(object sender, System.EventArgs e) { string filePath = Server.MapPath("\\Transcriber\\TranscriberTest\\"); string fileName =...
14
by: S N | last post by:
I am using the following code to hide the download url of files on my website. The code uses Response.Binarywrite to send file to the client. Kindly indicate the maximum size of the file that can be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.