473,396 Members | 1,895 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,396 software developers and data experts.

Efficient download script?

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

%>

Feb 13 '07 #1
2 2439

<ro****@oeffner.netwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
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.

Feb 13 '07 #2
Thanks Anthony. I'll give that a go.

Rob

On 13 Feb, 14:45, "Anthony Jones" <A...@yadayadayada.comwrote:
<rob...@oeffner.netwrote in message

news:11**********************@m58g2000cwm.googlegr oups.com...


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.- Hide quoted text -

- Show quoted text -

Feb 13 '07 #3

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

Similar topics

3
by: Rod | last post by:
Hi, I have a script that download any file to the user according to the parameters. I call the script like this: download.php?file=xxx/myfile.doc&file_short=myfile.doc My script is working...
4
by: D. Alvarado | last post by:
Hello, I would like to design a page that measures the user's download connection. Does anyone have an example link or script that might aid me in this task? Thanks, - Dave
3
by: Nathan Sokalski | last post by:
I have a webform that contains a button which I want to do three things: 1. Delete a record from a database 2. Let the user download a text file that is generated 3. Refresh the page to show...
12
by: s99999999s2003 | last post by:
hi I have a file which is very large eg over 200Mb , and i am going to use python to code a "tail" command to get the last few lines of the file. What is a good algorithm for this type of task...
2
by: Jan Paul van de Berg | last post by:
I have a piece of software that people can download and a third party promoting that software. In order for them to be able to count the number of downloads, I have to put a tracking code on my...
1
by: Joey Nolan | last post by:
Hi, I have a PDF file on my site that I want to distribute freely but I want to be able to contact anyone who downloaded it to warn them about mistakes and modification and new versions and...
6
by: Piotrekk | last post by:
Hi I have theoretical problem. In my web service project there was a need to implement file download/upload using WS. I have created UploadChunk and DownloadChunk methods which transfer files in...
1
by: Tim Jones | last post by:
I have a web site where we offer MP3 downloads (yes, they are legal!). I've written a PHP script using readfile() (or fpassthru()) that sends the file using HTTP headers (via various header()...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.