C and S Design.
Search Friendly Programming and Design



Get this Code: Click to download the ASP file.
Do not copy and paste the displayed code. The display function adds line breaks so what you see is definitely not what you would get.

Code for this Example. (File Download)


<%Response.Buffer = True%>
<%
dim dlFileName
if request.querystring("dl") <> "" _ 
	then
	dlFileName = request.querystring("dl")
end if
%>

<%
function GetPagePath()
dim ThisPath
ThisPath = _ 
	Split(Request.ServerVariables("PATH_INFO"), _ 
	"/")
GetPagePath = ThisPath(ubound(ThisPath)-1)
end function

sub SetDownload(strPath) ' FileName)
On Error Resume Next
' basic error checking
If strPath = "" Then
Response.Clear
Response.Write("No file specified.")
Response.End
ElseIf InStr(strPath, "..") > 0 Then
Response.Clear
Response.Write("Illegal folder location.")
Response.End
ElseIf Len(strPath) > 1024 Then
Response.Clear
Response.Write("Folder path too long.")
Response.End
Else
call DownloadFile(strPath)
End If
end sub

Sub DownloadFile(strPath)
'   declare variables
Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream
dim strFileName
dim FileName

strAbsFile = Server.MapPath(strPath)
Set objFSO = _ 
	Server.CreateObject("Scripting.FileSystemObject")
'make sure the file exists
If objFSO.FileExists(strAbsFile) Then
Set objFile = objFSO.GetFile(strAbsFile)
'-- first clear the response, and then set the _ 
	appropriate headers
Response.Clear
'Get the filename you give it will be the one that is _ 
	shown
'   to the users by default when they save
	
Response.AddHeader "Content-Disposition", _ 
	"attachment; filename=" & dlFileName
Response.AddHeader "Content-Length", _ 
	objFile.Size
Response.ContentType = _ 
	"application/octet-stream"
Set objStream = _ 
	Server.CreateObject("ADODB.Stream")
objStream.Open
'-- set as binary
objStream.Type = 1
Response.CharSet = "UTF-8"
'-- load into the stream the file
objStream.LoadFromFile(strAbsFile)
'-- send the stream in the response
Response.BinaryWrite(objStream.Read)
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
Else  'objFSO.FileExists(strAbsFile)
Response.Clear
Response.Write("No such file exists.")
End If
Set objFSO = Nothing
End Sub

%>



Valid HTML 4.01! Valid CSS! copyright © C and S Design 2004 - 2005
Website Design and SE Friendly Coding C and S Design