Last post Jun 30, 2008 10:19 PM by osbornm
Jun 16, 2008 04:16 PM|sdlyr8|LINK
Hey everyone, I am trying to write a page that will export a CSV file filled from a database. Unfortunately I haven't been able to even do a simple download of the file. All I want it to do now is force the browser to open the dialog box for the download
instead of displaying the file in the browser. Everywhere I read I see that AppendHeader("Content-Disposition","attachment; filename=test.csv") should force the dialog, but it isn't in my case. My only idea is that because this is running on a dev box, it
doesn't actually have Microsoft Office (including Excel), and so it doens't know what a CSV file is. Anyone know if this is it or am I doing something wrong?
<%@ Page Language="VB" Debug="True" %>
Response.ContentType = "text/csv"
Response.TransmitFile( Server.MapPath("test.csv") )
EDIT: I just tried running the script in Firefox and it worked perfectly, so I'm guessing it's something in IE. Hopefully that helps some...
Jun 16, 2008 04:44 PM|osbornm|LINK
if the MIME type isn't registered with the broswer you wont be able to force a download.
Jun 16, 2008 04:54 PM|sdlyr8|LINK
ok I checked the registry for MIME types and correct, Text/CSV was not there. But, I changed it to a simply Text/plain (which is there), hoping I could at least export a text file, but that still does not work, it still displays it in the browser. I've had
it working with downloading images, but any text file will not work.
Jun 16, 2008 06:22 PM|osbornm|LINK
IE default behavior for is to just display the text...
IE default behavior that is why it was just displayign in the browers and text\plain..
Here is code to get it working though, i tested this myself
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=file.txt");
Jun 16, 2008 06:56 PM|osbornm|LINK
did that new code work for you?
Jun 26, 2008 11:30 PM|Beau_Damore|LINK
I am doing the same sort of thing. I build a .csv with StreamWriter and used your code aboe but I get all the page's aspx code too in the export file, not just the comma delimited text.
Here's my code, what am I doing wrong? Thanks:
Protected Sub ButtonExportUsers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonExportUsers.Click
'get the User table info===========================
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim strSQL As String
Dim connStr As String
connStr = System.Configuration.ConfigurationManager.ConnectionStrings("LandingConnectionString").ConnectionString
connStr += ";Provider=SQLOLEDB"
myConnection = New OleDbConnection(connStr)
strSQL = "TIOCGetAllUserInfo"
myCommand = New OleDbCommand(strSQL, myConnection)
Dim dbReader = myCommand.ExecuteReader()
'Open a file for writing to be used in the following dbReader loop=====================
Dim FILENAME As String = Server.MapPath("Export/TIOCUsers.csv")
'Get a StreamReader class that can be used to read the file
If File.Exists(FILENAME) Then
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(FILENAME)
Dim vLinetoAppend As String = ""
vLinetoAppend = ""
If (Not dbReader("FirstName") Is DBNull.Value And Not dbReader("LastName") Is DBNull.Value _
And Not dbReader("Email") Is DBNull.Value) Then
vLinetoAppend += dbReader("FirstName") & ","
vLinetoAppend += dbReader("LastName") & ","
vLinetoAppend += dbReader("Email")
'Append the the end of the string
'close the reader and connecection
'Close the stream
'force download to client==================
Response.ContentType = "text/plain"
Jun 30, 2008 10:19 PM|osbornm|LINK
You need to tell it to close the response object and not write anything else... This code should fix it for you.
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", "attachment; filename=file.csv");