nLL Mobile web, .Net, Android, gadgets and some random stuff

classic asp large file download code

2. December 2010 19:48 by nolovelust in Useful

I've found this in my old stuff. As you would know on iis there is 4 mb file size limit. If you try to send a file larger than 4 mb to visitor with asp you'll see that it will fail with error response buffer overflow.

Use below code to prevent that error

 

Server.ScriptTimeout = 900 
Response.Buffer = False 
Response.ContentType="YOURMIMETYPE"
Set adoStream = CreateObject("ADODB.Stream")
response.addheader "Content-Disposition", "attachment;filename="& filename & ";"
chunk = 2048
adoStream.Open() 
adoStream.Type = 1 
adoStream.LoadFromFile "FILEPATH" 
iSz = adoStream.Size 
Response.AddHeader "Content-Length", iSz 
For i = 1 To iSz \ chunk 
If Not Response.IsClientConnected Then Exit For 
Response.BinaryWrite adoStream.Read(chunk) 
Next 
If iSz Mod chunk > 0 Then 
If Response.IsClientConnected Then 
Response.BinaryWrite adoStream.Read(iSz Mod chunk) 
End If 
End If 
adoStream.Close 
Set adoStream = Nothing 
Response.End