Missing Web log Entry's for file downloads

Discussion in 'ASP .Net' started by AOTX San Antonio, Jul 24, 2008.

  1. Hi,

    I have been using the code (some of it has been removed for simplicity)
    below to allow authenticated (using ASP.NET membership database) users to get
    a file from their archive area. It seems to work fine, however I noticed
    that no web log entry is added when a successful download occurs (normally a
    200 HTTP status code, however, if there is an authorization failure, it gets
    logged). I have a logging routine that logs a successful download to a
    database, but the W3SVC IIS log has nothing. The code below is contained in
    an .ashx file. One other thing to note, I have added MADAM to the web
    application, to allow for basic authentication (they might use WGET to
    download archives, instead of the web forms).

    Thanks.

    Public Class GetArchive : Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements
    IHttpHandler.ProcessRequest
    Dim strFileName As String = String.Empty
    Dim blnTransmittingFile As Boolean = False
    Try
    context.Response.Buffer = True
    If context.User.Identity.IsAuthenticated = True Then
    strFileName = context.Request.QueryString("file")
    If String.IsNullOrEmpty(strFileName) = False Then
    strFileName = System.IO.Path.GetFileName(strFileName)
    context.Response.Clear()

    Dim objFileInfo As New System.IO.FileInfo(strFileName)
    Dim intFileSize As Long = 0

    If objFileInfo IsNot Nothing Then
    intFileSize = objFileInfo.Length
    End If

    context.Response.StatusCode = 200
    context.Response.ContentType = "application/zip"
    context.Response.AddHeader("Content-Disposition",
    "attachment; filename=" + strFileName)
    context.Response.AddHeader("Content-Length",
    intFileSize.ToString())

    If context.Request.HttpMethod <> "HEAD" Then
    Dim dtStart As DateTime = Now()
    Dim dtFinish As DateTime
    Dim tsTimeTaken As TimeSpan
    context.Response.SuppressContent = False
    context.Response.Buffer = False
    context.Response.BufferOutput = False
    blnTransmittingFile = True
    context.Response.TransmitFile(strFileName, 0, -1)
    context.Response.Flush()
    context.Response.Close()
    dtFinish = Now()
    tsTimeTaken = dtFinish - dtStart
    EventLogger.LogDownloadEvent(strFileName,
    strUserName, tsTimeTaken.TotalSeconds)
    End If
    Else
    context.Response.Clear()
    context.Response.ContentType = "text/plain"
    context.Response.StatusCode = 404
    context.Response.Write("File Not Found")
    End If
    End If

    Catch ex As Exception
    If blnTransmittingFile = False Then
    context.Response.Clear()
    context.Response.ClearContent()
    context.Response.ClearHeaders()
    context.Response.ContentType = "text/plain"
    End If
    context.Response.StatusCode = 403
    context.Response.Write("Request failed [" + ex.ToString() + "]")
    EventLogger.LogDownloadEvent("Failed", ex.ToString())
    End Try
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements
    IHttpHandler.IsReusable
    Get
    Return False
    End Get
    End Property
    End Class
     
    AOTX San Antonio, Jul 24, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AtomicBob
    Replies:
    14
    Views:
    966
    Toby Inkster
    May 2, 2006
  2. AOTX San Antonio
    Replies:
    7
    Views:
    458
    Allen Chen [MSFT]
    Sep 8, 2008
  3. Mike Owen

    Allowing entry of a Carriage Return during data entry

    Mike Owen, Jul 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    797
    Alessandro Zifiglio
    Jul 27, 2006
  4. Replies:
    0
    Views:
    1,418
  5. Noozer
    Replies:
    2
    Views:
    303
    Dr John Stockton
    Aug 1, 2005
Loading...

Share This Page