locked
File download in a separate class RRS feed

  • Question

  • User-1919454513 posted

    How can I move the following to a separate class for repeated use ? (the problem is that it uses response object)

    Many thanks ...

    ' Download/View a given file

    Protected Sub downloadFile(ByVal sFile As String)

    Dim name As String = Path.GetFileName(sFile)

    Dim ext As String = Path.GetExtension(sFile)

    Dim sFileType As String = ""

    ' set known types based on file extension

    If Not (ext Is Nothing) Then

    sFileType = Utils.getMimeTypeByExtention(ext)

    End If

    response.AppendHeader("content-disposition", "attachment; filename=" + name)

    If (sFileType.Length > 0) Then

    Response.ContentType = sFileType

    End If

    Try

    Response.WriteFile(sFile)

    Response.End()

    Catch ex As Exception

    End Try

    End Sub

    Thursday, February 12, 2009 5:24 PM

Answers

  • User1817007508 posted

    Hi,

    Prefix HttpContext.Current.

    like this

    HttpContext.Current.Response.WriteFile(sFile)

    HttpContext.Current.Response.End()

    HttpContext.Response.ContentType

    Hope this may help

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 13, 2009 12:07 AM