How to break up binaryWrite file into multiple chunks

K

Katie

Hi,

I am doing a binaryWrite to allow users to download files. The problem
occurs if the file is too big. Some of the files i have are close to
100 megs. I read on msdn that if the data is greater than 4MB it is
advisable to break it up into multiple chunks

http://msdn.microsoft.com/library/d...html/04d5f44f-cc63-4465-b45f-634b95130b4a.asp

How would that be done?
Thanks for your time and help :)

ps below is the sub i use for streaming



Private Sub streamDocs(path, filename, originalFileName, contentType)


Response.AddHeader
"content-disposition","attachment;filename="&originalFileName
Response.ContentType = contentType
Dim BinaryStream, Fil, fs
Set BinaryStream = CreateObject("ADODB.Stream")


set fs = Server.CreateObject("Scripting.FileSystemObject")


Set Fil = fs.GetFile(path & "\" &filename) 'Open file


BinaryStream.Type = 1
BinaryStream.Open
BinaryStream.LoadFromFile Fil.path
Response.BinaryWrite BinaryStream.Read
BinaryStream.Cancel
BinaryStream.Close
set BinaryStream = nothing


End sub
 
E

Evertjan.

Justin Piper wrote on 13 sep 2006 in
microsoft.public.inetserver.asp.general:
Whoops, that should of course read, "For i = 0 To BinaryStream.Size
Step 1000".

or:

For i = 0 To BinaryStream.Size/1000
 
E

Evertjan.

Justin Piper wrote on 13 sep 2006 in
microsoft.public.inetserver.asp.general:
Well, as long as we're splitting hairs, using "Step" is about 30%
faster.
:)

Oh, if you wish, try:

temp = int(BinaryStream.Size/1000)+1
For i = 0 To temp

However I do not believe you are right, Justin, with the 30%,
as BinaryStream.Read(1000) and the response.write
will take much more cpu time,
than the repetitive for-next division by 1000 overhead.

I guess it will be muh ess than 1%.
 
K

Katie

Hi,

Thanks for your help.
It works :)
Just wondering is there a particular value for the data size that is
optimal or can i use any e.g. 1000kb

Thanks
:)
 
A

Anthony Jones

Katie said:
Hi,

Thanks for your help.
It works :)
Just wondering is there a particular value for the data size that is
optimal or can i use any e.g. 1000kb

Katie,

My apologies I did have your earlier post on this subject marked for a
response but I never did get round it.

Unfortunately the code posted so far contains a small bug that will result
in a truncation of the file to the nearest mutliple of the chunk size.

Const mclChunkSize = &H200000

Dim i

Response.Buffer = False

For i = 0 To BinaryStream.Size Step mclChunkSize
Response.BinaryWrite BinaryStream.Read(mclChunkSize)
Next

If (BinaryStream.Size Mod mclChunkSize) > 0 Then
Response.BinaryWrite BinaryStream.Read(BinaryStream.Size Mod
mclChunkSize)
End If


Choice of chunk size will affect the download performance. You shoud choose
a good size like the 1Mb you proposed. Using a very small chunk size such
as 1000 is inefficient (even a single TCP/IP packet can carry a larger
payload) and each Response.BinaryWrite will block until all acknowledgements
for data sent has been received from the client. By using a larger chunk
size you let the TCP/IP stack do it's work far more effeciently.

Justin has pointed out there is a trade off between chunksize and server
memory use. However, it's worth bearing in mind that by default only 10
requests will be processed per CPU anyway (although this may be increased it
shouldn't really exceed 25) hence a chunksize of 2MB only represents 20MB -
50MB per CPU and that assumes all worker threads are busy responding to
download requests.

Anthony.



 
K

Katie

Thanks for your help :)

Justin said:
Agh. This has not been my week. That should be:

If Not stream.EOS Then Response.BinaryWrite stream.Read

It only appeared to work since Response.Write will dutifly write nothing
if you tell it to. :p
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top