Data Export / Download Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn Mehaffie

    #1

    Data Export / Download Problem

    I have the following class that I've wirtten to take a Dataset and
    automatically export it to either XML, ASCII or Tab delimited file. The
    reason I wrote it they way I did was that I don't want to have o create a
    temporary file on the web server everytime someone want to run and export.
    When I call the ExportToBrowser function the user gets the standard
    open/save file dialog box. If the user chooses to save the file the program
    works fine, but if they choose to open the file they get to an error the the
    file does not exist in the tempory internet file directory. How can I get
    this code to work as if the user had clicked on a link directly to a file on
    the web server? Is there a command that I can use to write the file to the
    temporary internet file so that if the user choses to open the file they
    program works? Any help will be greatly appreciated.
    Below is the code that is in the class I wrote to accomplish the task above.

    Imports System.Text

    Public Class DBExport
    Private _DataSet As New DataSet
    Private _IncludeHeader As Boolean = True
    Private _IncludeXMLSche ma As Boolean = False
    Private _ExportType As Export_Type
    Private _FileName As String

    Public Enum Export_Type
    XML = 0
    ASCII = 1
    TAB = 2
    End Enum

    Public Property DataSet() As DataSet
    Get
    Return _DataSet
    End Get
    Set(ByVal Value As DataSet)
    _DataSet = Value
    End Set
    End Property

    Public Property IncludeHeader() As Boolean
    Get
    Return _IncludeHeader
    End Get
    Set(ByVal Value As Boolean)
    _IncludeHeader = Value
    End Set
    End Property

    Public Property IncludeXMLSchem a() As Boolean
    Get
    Return _IncludeXMLSche ma
    End Get
    Set(ByVal Value As Boolean)
    _IncludeXMLSche ma = Value
    End Set
    End Property

    Public Property ExportType() As Export_Type
    Get
    Return _ExportType
    End Get
    Set(ByVal Value As Export_Type)
    _ExportType = Value
    End Set
    End Property

    Public Property FileName() As String
    Get
    Return _FileName
    End Get
    Set(ByVal Value As String)
    _FileName = Value
    End Set
    End Property

    Public Function ExportToBrowser (ByVal CurrentResponse As
    System.Web.Http Response) As String
    Select Case _ExportType
    Case Is = Export_Type.XML
    'Setup response to export data
    CurrentResponse .AppendHeader(" content-disposition", "attachment ;
    filename=" & _FileName & ".xml")
    CurrentResponse .ContentType = "Text/XML"

    'Write schema to export
    If _IncludeXMLSche ma Then
    CurrentResponse .Write(_DataSet .GetXmlSchema)
    End If

    'Write data to export
    CurrentResponse .Write(_DataSet .GetXml())
    Case Is = Export_Type.ASC II
    'Setup response to export data
    CurrentResponse .AppendHeader(" content-disposition", "attachment ;
    filename=" & _FileName & ".txt")

    If _IncludeHeader Then
    CurrentResponse .Write(GetHeade r())
    End If

    CurrentResponse .Write(GetData( ))
    Case Is = Export_Type.TAB
    'Setup response to export data
    CurrentResponse .AppendHeader(" content-disposition", "attachment ;
    filename=" & _FileName & ".xls")

    If _IncludeHeader Then
    CurrentResponse .Write(GetHeade r())
    End If

    CurrentResponse .Write(GetData( ))
    End Select

    CurrentResponse .End()
    End Function

    Private Function GetHeader() As String
    Dim iXcounter As Integer
    Dim sbHeader As New StringBuilder
    Dim stDelimiter As String

    If _ExportType = Export_Type.ASC II Then
    stDelimiter = ", "
    Else
    stDelimiter = Chr(9)
    End If

    For iXcounter = 0 To _DataSet.Tables (0).Columns.Cou nt - 1

    sbHeader.Append (_DataSet.Table s(0).Columns(iX counter).Column Name)

    If iXcounter <> _DataSet.Tables (0).Columns.Cou nt - 1 Then
    sbHeader.Append (stDelimiter)
    Else
    sbHeader.Append (Chr(13))
    End If
    Next

    'If Tab delimited add extra line feed so there is a blank line
    between the header and first
    'record of data
    If _ExportType = Export_Type.TAB Then
    sbHeader.Append (Chr(13))
    End If

    Return sbHeader.ToStri ng()
    End Function

    Private Function GetData() As String
    Dim iXcounter As Integer
    Dim iYcounter As Integer
    Dim sbData As New StringBuilder
    Dim stDelimiter As String

    If _ExportType = Export_Type.ASC II Then
    stDelimiter = ", "
    Else
    stDelimiter = Chr(9)
    End If

    For iXcounter = 0 To _DataSet.Tables (0).Rows.Count - 1
    For iYcounter = 0 To
    _DataSet.Tables (0).Rows(iXcoun ter).ItemArray. GetUpperBound(0 )

    sbData.Append(_ DataSet.Tables( 0).Rows(iXcount er).ItemArray.G etValue(iYcount e
    r))
    If iYcounter <> _DataSet.Tables (0).Columns.Cou nt - 1 Then
    sbData.Append(s tDelimiter)
    Else
    sbData.Append(C hr(13))
    End If
    Next
    Next

    Return sbData.ToString ()
    End Function
    End Class

    Thanks,

    Shawn Mehaffie
    PC Resources, LLC


Working...