• home
  • forum
  • my
  • kt
  • download
  • Work with If-Modified-Since and Last-Modified in ASP.Net.

    Author: 2007-07-04 15:00:29 From:

    Sometimes you will need to send static files over http using ASP.Net - for example if you want to control access to the file programmatically. Such tasks can be simply handled by Response.WriteFile(FileName) for plain html files, but there are two problems:
    • Response.ContentType is "text/html" by default. So if you want to write .gif images by ASPX, you have to set right Content-Type http header according to the file type (image/gif).
    • There is a good idea to handle at least Last-Modified and If-Modified-Since headers to reduce traffic and increase performance. Otherways, the file is sent each time.
    I created a simple VB.Net support functions for this tasks.
    1. GetContentType - returns content-type of a file from registry
    2. DateFromHTTP - converts string date from http header (Fri, 09 Sep 2005 07:51:28 GMT format) to Date format
    3. DateToHTTPDate - converts date to string format.
      Function SendFileOverHTTP(ByVal FileName) As String
        'Get If-Modified-Since header from request.
        Dim sIfModifiedSince As String = Request.Headers("If-Modified-Since")
    
        'Convert the header To date.
        Dim IfModifiedSince As Date = DateFromHTTP(sIfModifiedSince)
    
        'Get a time when the file was last modified.
        Dim LastModified As Date = FileDateTime(FileName).ToUniversalTime
    
        'round the date To seconds.
        LastModified = New Date(LastModified.Year, LastModified.Month, LastModified.Day, _
          LastModified.Hour, LastModified.Minute, LastModified.Second)
    
        'Check If the last modified time of the file is greater than If-Modified-Since
        If LastModified > IfModifiedSince Then '200 OK - file was modified.
          Response.StatusCode = "200"
          Response.StatusDescription = "OK"
    
          'Set Last-Modified header
          Response.AddHeader("Last-Modified", DateToHTTPDate(LastModified))
    
          Dim ContentType As String = GetContentType(FileName)
          'Set Content-type http header (from registry)
          Response.ContentType = ContentType
          Response.WriteFile(FileName)
          Return "Send: " & FileLen(FileName) & "," & ContentType & "," & FileName
        Else '304 - file is Not modified.
          Response.StatusCode = "304"
          Response.StatusDescription = "Not Modified"
          Return "304: " & LastModified & "," & IfModifiedSince & "," & FileName
        End If
      End Function
    
    
    
    'Support functions:
      'returns a content-type of the file by file extensions
      Public Function GetContentType(ByRef FileName As String) As String
        'get the file ext
        Dim Ext As String = System.IO.Path.GetExtension(FileName)
    
        'open a the file type registry key 
        Dim Reg As Microsoft.Win32.RegistryKey = _
          Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Ext)
    
        'read Content Type value.
        Return Reg.GetValue("Content Type")
      End Function
    
    
      Public Function GetFileLastModified(ByVal FileName) As String
        Return DateToHTTPDate(FileDateTime(FileName))
      End Function
    
    
    
      'Converts date (19991022 11:08:38)
      'to http form (Fri, 22 Oct 1999 12:08:38 GMT)
      Function DateToHTTPDate(ByVal OleDATE As Date) As String
        On Error Resume Next
        OleDATE = OleDATE.ToUniversalTime
        Return engWeekDayName(OleDATE) & _
         ", " & Right("0" & Day(OleDATE), 2) & " " & engMonthName(OleDATE) & _
         " " & Year(OleDATE) & " " & Right("0" & Hour(OleDATE), 2) & _
         ":" & Right("0" & Minute(OleDATE), 2) & ":" & Right("0" & Second(OleDATE), 2) & " GMT"
      End Function
    
      Function engWeekDayName(ByVal dt As Date) As String
        Dim Out As String
        Select Case WeekDay(dt, 1)
          Case 1 : Out = "Sun"
          Case 2 : Out = "Mon"
          Case 3 : Out = "Tue"
          Case 4 : Out = "Wed"
          Case 5 : Out = "Thu"
          Case 6 : Out = "Fri"
          Case 7 : Out = "Sat"
        End Select
        Return Out
      End Function
    
      Function engMonthName(ByVal dt As Date) As String
        Dim Out As String
        Select Case Month(dt)
          Case 1 : Out = "Jan"
          Case 2 : Out = "Feb"
          Case 3 : Out = "Mar"
          Case 4 : Out = "Apr"
          Case 5 : Out = "May"
          Case 6 : Out = "Jun"
          Case 7 : Out = "Jul"
          Case 8 : Out = "Aug"
          Case 9 : Out = "Sep"
          Case 10 : Out = "Oct"
          Case 11 : Out = "Nov"
          Case 12 : Out = "Dec"
        End Select
        Return Out
      End Function
    
      Public Function DateFromHTTP(ByVal HTTPDate As String) As Date
        Dim Swd As String, d As String, Sm As String, y As String, h As String
        Dim m As String, s As String, g As String, Out As Date
        HTTPDate = LCase$(HTTPDate)
    
        If Mid$(HTTPDate, 27, 3) = "gmt" Then
          Swd = Left$(HTTPDate, 3)
          d = Mid$(HTTPDate, 6, 2)
          Sm = Mid$(HTTPDate, 9, 3)
          y = Mid$(HTTPDate, 13, 4)
          h = Mid$(HTTPDate, 18, 2)
          m = Mid$(HTTPDate, 21, 2)
          s = Mid$(HTTPDate, 24, 2)
          Out = New Date(y, mFromSm(Sm), d, h, m, s)
          Out = Out.ToLocalTime
        End If
    
        Return Out
      End Function
    
      Function wdFromSwd(ByVal Swd As String) As Integer
        Dim Out As Integer
        Select Case LCase$(Swd)
          Case "sun" : Out = 1
          Case "mon" : Out = 2
          Case "tue" : Out = 3
          Case "wed" : Out = 4
          Case "thu" : Out = 5
          Case "fri" : Out = 6
          Case "sat" : Out = 7
        End Select
        Return Out
      End Function
    
      Function mFromSm(ByVal Sm As String) As Integer
        Dim Out As Integer
        Select Case LCase$(Sm)
          Case "jan" : Out = 1 : Case "feb" : Out = 2
          Case "mar" : Out = 3 : Case "apr" : Out = 4
          Case "may" : Out = 5 : Case "jun" : Out = 6
          Case "jul" : Out = 7 : Case "aug" : Out = 8
          Case "sep" : Out = 9 : Case "oct" : Out = 10
          Case "nov" : Out = 11 : Case "dec" : Out = 12
        End Select
        Return Out
      End Function
    
    

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      NET (110)

    New

    Hot