473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

File Export / Download Question

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 _IncludeXMLSchema 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 IncludeXMLSchema() As Boolean
Get
Return _IncludeXMLSchema
End Get
Set(ByVal Value As Boolean)
_IncludeXMLSchema = 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.HttpResponse) 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 _IncludeXMLSchema Then
CurrentResponse.Write(_DataSet.GetXmlSchema)
End If

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

If _IncludeHeader Then
CurrentResponse.Write(GetHeader())
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(GetHeader())
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.ASCII Then
stDelimiter = ", "
Else
stDelimiter = Chr(9)
End If

For iXcounter = 0 To _DataSet.Tables(0).Columns.Count - 1
sbHeader.Append(_DataSet.Tables(0).Columns(iXcount er).ColumnName)

If iXcounter <> _DataSet.Tables(0).Columns.Count - 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.ToString()
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.ASCII 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(iXcounter).ItemArray.GetUp perBound(0)
sbData.Append(_DataSet.Tables(0).Rows(iXcounter).I temArray.GetValue(iYcounter))
If iYcounter <> _DataSet.Tables(0).Columns.Count - 1 Then
sbData.Append(stDelimiter)
Else
sbData.Append(Chr(13))
End If
Next
Next

Return sbData.ToString()
End Function
End Class

Thanks,

Shawn Mehaffie
PC Resources, LLC

Nov 20 '05 #1
0 1489

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
5
by: Brad | last post by:
In several aspx applications I export crytal reports to pdf, xls and doc files and then the aspx page writes the selected export file to the client browser. This all works with one small quirk: ...
1
by: Peter D.C. | last post by:
Hi On an asp.net page I have a search section that, based on the search criteria, populates a datagrid. The search is done trought a stored procedure and a resulting dataset with only one tabel...
5
by: Tim_Mac | last post by:
hi, i read that by adding the following code to by aspx pages, it would not store temporary internet files: Response.Cache.SetCacheability(HttpCacheability.NoCache); it didn't actually work...
1
by: Bernard Goh | last post by:
Hi All, I am newbie for ASP.NET programming and I am having this error when I tried to print a crytal report from my dotnet web application. Error Message : Invalid file name. at...
0
by: ar | last post by:
Hello, In IE I disable "Automatic prompting for file downloads" which causes the IE information bar to show up when I try to push a file download from an iframe. I want to keep this behaviour. ...
2
by: =?Utf-8?B?Tmc=?= | last post by:
Hi. have someone can give me some idea how to start. the thing i want do is using web page export the sql data to text file then let user download. eg. the webpage has one button. when user...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
1
by: bienwell | last post by:
Hi all, I'm going to develop an ASP.net page (VB) that allows users to export data into the zip file. Do you have any sample codes? Please give me the source codes if you have. One more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.