I came across a situation where people where “leaching” images off my family photo site. “File Leaching” means that instead of the person downloading the file, and simply serving it up from their server, they were simply linking to my version of it.
While this may seem trivial, it happened on a site that has 10,000 family pictures, and was causing serious bandwidth issues for me.
So… being the good developer I think I am, I came up with a way to prevent that.
The basics of it are, simply store your files above the websites root directory, and stream them to the user upon request.
I found using ADO.Stream this is possible, and very efficient.
Hope you find it as useful as I do… Enjoy!
Usage:
-
<%
-
‘*****************************************************************************
-
‘*****************************************************************************
-
‘
-
‘ This code can be used anywhere you like, all I ask is that you keep this
-
‘ notice here, so people know who actually made it! =D Thanks!
-
‘
-
‘ This code was developed by Kevin Pirnie, c/o o7th Web Design
-
‘ support@07th.com :: http://www.07th.com
-
‘
-
‘*****************************************************************************
-
‘*****************************************************************************
-
Dim strPath : strPath = "C:\WebStorage\thefile.zip" ‘physical path to the file
-
Dim strFileName : strFileName = "WhatIWantToCallIt.zip" ‘whatever I feel like naming the file
-
Call downloadFile(strPath, strFileName)
-
%>
Code:
-
<%
-
‘Force File Download
-
Sub DownloadFile(ByVal strFileName, ByVal strFileNamePass, ByVal intFileType)
-
Response.Buffer = True
-
Response.Clear
-
Set objStream = CreateObject("ADODB.Stream")
-
objStream.Open
-
objStream.Type = 1
-
Set objFilesystem = CreateObject("Scripting.FileSystemObject")
-
If Not objFilesystem.FileExists(strFilename) Then
-
Write("<h1>Error</h1>: " & strFilename & " does not exist<p>")
-
Response.End
-
End If
-
Set objFilestream = objFilesystem.GetFile( strFilename )
-
intFilelength = objFilestream.size
-
objStream.LoadFromFile( strFilename )
-
If Err Then
-
Write("<h1>Error: </h1>" & Err.Description & "<p>")
-
Response.End
-
End If
-
If Len( Trim(strDownloadFilename) ) > 0 Then
-
strFileNamePass = Trim( strFileNamePass )
-
Else
-
strFileNamePass = objFilestream.Name
-
End If
-
Select Case intFileType
-
Case 1 ‘CSV
-
Response.ContentType = "text/csv"
-
Case 2 ‘XML
-
Response.ContentType = "text/xml"
-
End Select
-
Response.AddHeader "Content-Disposition", "attachment; filename=" & strFileNamePass
-
Response.AddHeader "Content-Length", intFilelength
-
Response.Charset = "UTF-8"
-
For i = 0 To objStream.Size
-
i = i + 128000
-
Response.BinaryWrite(objStream.Read(128000))
-
Response.Flush
-
Next : i = Null
-
Set objFilestream = Nothing
-
Set objFilesystem = Nothing
-
End Sub
-
%>












