Efficient download script?

R

robert

Hi,
I'm new to ASP but do know my way around VB and a bit of VB script. MY
ISP have ASP enabled for my website. So I'm pondering on how to write
an efficient script for users to download a few files from my website.
Having read posts on this and other newsgroups here's what I've got so
far. I have resorted to chop the download in chunks as it appears that
my ISP does not permit large files to be streamed out.

What I would like to know is if the code below can be made more
efficient (smaller or larger chunk size) or if there are some other
fundamental flaws I've overlooked.

Thanks,

Rob



<%
dim oFSO, oFile, objStream, strfilepath, filename
Const ForWriting = 2, ForAppending = 8

filename = Request.QueryString("file")

strFilePath=server.mappath("..\..\Bin\" & filename)
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(strfilepath)
strFileSize = f.size
set f=nothing: set fso=nothing
Const adTypeBinary = 1
Response.Clear
Response.Expires = 0

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "application/exe"
Response.AddHeader "Content-Disposition", "attachment; filename=" &
filename
Response.AddHeader "Content-Length", strFileSize
Response.ContentType = strFileType

Dim NumberofBlocks, chunk, lBlocks, fsize
fsize = strFileSize
sentbytes = 0
chunk = 16384
NumberofBlocks=(strFileSize -(strFileSize mod chunk))/chunk
For lBlocks = 1 To NumberofBlocks
If Response.IsClientConnected = False Then Exit For
Response.BinaryWrite objStream.Read(CHUNK)
Response.Flush

' sentbytes = sentbytes + chunk
Next
strFileSize = strFileSize Mod CHUNK
If strFileSize > 0 And Response.IsClientConnected = True Then
Response.BinaryWrite objStream.Read(strFileSize)
Response.Flush
' sentbytes = sentbytes + strFileSize
end if

objStream.Close
set objStream = Nothing
Set objFile = Nothing
'response.end

%>
 
A

Anthony Jones

Hi,
I'm new to ASP but do know my way around VB and a bit of VB script. MY
ISP have ASP enabled for my website. So I'm pondering on how to write
an efficient script for users to download a few files from my website.
Having read posts on this and other newsgroups here's what I've got so
far. I have resorted to chop the download in chunks as it appears that
my ISP does not permit large files to be streamed out.

What I would like to know is if the code below can be made more
efficient (smaller or larger chunk size) or if there are some other
fundamental flaws I've overlooked.

Thanks,

Rob

Here is a function you can use:-

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

The buffer limit is likely 4MB the default on an IIS6 server. The 16K chunk
size you had in the original code is too small. Also by using
Response.Buffer = False you do not need the .flush command.

There is no need to set the content-length header. The server will use
chunked encoding which prefixes each chunk with it's size and the
content-length header is implied as the total of these chunks.
 
R

robert

Thanks Anthony. I'll give that a go.

Rob



Here is a function you can use:-

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

The buffer limit is likely 4MB the default on an IIS6 server. The 16K chunk
size you had in the original code is too small. Also by using
Response.Buffer = False you do not need the .flush command.

There is no need to set the content-length header. The server will use
chunked encoding which prefixes each chunk with it's size and the
content-length header is implied as the total of these chunks.- Hide quoted text -

- Show quoted text -
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top