Web Analytics Made Easy -
StatCounter Detecting File Download - CodingForum

Announcement

Collapse
No announcement yet.

Detecting File Download

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting File Download

    I need to detect a file download event. Once a file is downloaded from a web page link, I need to send a confirmation email to the file's uploader. Sending the file is no problem, but how do I detect the download event?

  • #2
    You don't You get ASP to manage the download for you:

    Code:
    <%
    Response.Buffer = True
    
    function getFileExt(sFilePath)
    'returns all chars after (and excluding) the right-most instance of "." in sFilePath
    	dim rv : rv = ""
    	if sFilePath & "" <> "" then
    		rv = mid(sFilePath, inStrRev(sFilePath, ".")+1)
    	end if
    	getFileExt = rv
    end function
    
    
    dim sDownloadAs, sVirtualPath, sFullPath, sFileExt, sContentType, iFileSize
    sDownloadAs		= request.querystring("dname")	'	Name it'll offer to save file as
    sVirtualPath	= request.querystring("file")	'	File location on server
    sFullPath		= server.mappath(sVirtualPath)
    sFileExt		= getFileExt(sVirtualPath)
    select case lcase(sFileExt)
    	case "asf"
    		sContentType = "video/x-ms-asf"
    	case "avi"
    		sContentType = "video/avi"
    	case "doc"
    		sContentType = "application/msword"
    	case "zip"
    		sContentType = "application/zip"
    	case "xls"
    		sContentType = "application/vnd.ms-excel"
    	case "gif"
    		sContentType = "image/gif"
    	case "jpg", "jpeg"
    		sContentType = "image/jpeg"
    	case "wav"
    		sContentType = "audio/wav"
    	case "mp3"
    		sContentType = "audio/mpeg3"
    	case "mpg", "mpeg"
    		sContentType = "video/mpeg"
    	case "rtf"
    		sContentType = "application/rtf"
    	case "htm", "html"
    		sContentType = "text/html"
    	case "asp"
    		sContentType = "text/asp"
    	case else
    		'Handle all other files
    		sContentType = "application/octet-stream"
    end select
    
    dim oFSO, oFile, blnFileFound
    blnFileFound = false
    set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    if oFSO.FileExists(sFullPath) then
    	set oFile = oFSO.GetFile(sFullPath)
    		iFileSize = oFile.Size
    	set oFile = nothing
    	blnFileFound = true
    end if
    set oFSO = nothing
    
    
    if blnFileFound then 
    
    dim oStream
    set oStream = Server.CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1 'adTypeBinary
    oStream.LoadFromFile sFullPath
    
    Response.Clear
    Response.AddHeader "Content-Disposition", "attachment; filename=" & sDownloadAs
    Response.AddHeader "Content-Length", iFileSize
    Response.Charset = "UTF-8"
    Response.ContentType = sContentType
    Response.BinaryWrite oStream.Read
    Response.Flush
    
    oStream.Close
    set oStream = nothing
    
    end if

    Save that as download.asp and link to it via:

    <a href="download.asp?file=myfile.zip&dname=myfile.zip">Download myfile.zip</a>

    You can then put whatever other ASP code you want into download.asp, before the downloady bit.

    Comment

    Working...
    X