fifo123
asked on
Exporting to a csv from ASP
Hi all,
I have a web app that generates html reports. After I generate the reports I go ahead and create a csv file for the reports behind the scenes and save the csv file on the server.
On the report page, at the bottom, there's a button "export" that when a user clicks on it would bring up a "file download" dialog box and lets the user save the csv file on their machine. This works fine on win 2000 but on windows 98 and window xp, the csv file gets opened within the browser. Is there a way to prevent that from happening??
Here is the code for the "export" button....
<a href="../Reports/Exports/t est.csv">< img type="image" alt="Export" src="../images/export.gif" ></a>
I have a web app that generates html reports. After I generate the reports I go ahead and create a csv file for the reports behind the scenes and save the csv file on the server.
On the report page, at the bottom, there's a button "export" that when a user clicks on it would bring up a "file download" dialog box and lets the user save the csv file on their machine. This works fine on win 2000 but on windows 98 and window xp, the csv file gets opened within the browser. Is there a way to prevent that from happening??
Here is the code for the "export" button....
<a href="../Reports/Exports/t
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
fifo,
This problem does occur while exporting.
The safe way is to call a javascript function through you a href...like,
<a href="javascript:export(); "><img type="image" alt="Export" src="../images/export.gif" ></a>
in that function
You submit the values to spool.asp
In spool.asp you create a document and give the user file save as option...etcc..
After all ur work there redirect again to your parent asp page..using response.redirect
I think this will solve the problem
This problem does occur while exporting.
The safe way is to call a javascript function through you a href...like,
<a href="javascript:export();
in that function
You submit the values to spool.asp
In spool.asp you create a document and give the user file save as option...etcc..
After all ur work there redirect again to your parent asp page..using response.redirect
I think this will solve the problem
hi fifo,
In order to stay in the same page, follow the directives
1. In the hyperlink tag, instead of giving the URL, call a Javascript method, that infact submits the form to the href location.( spool.asp)
2. Add the following code in the spool.asp file
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=test.csv"
If this is followed, on clicking the Export Image, the javascript function is called, which submits the form. As the form is submitted to a page with a content-type that IE cannot interpret, if would pop out the File Save as Dialog.
In order to stay in the same page, follow the directives
1. In the hyperlink tag, instead of giving the URL, call a Javascript method, that infact submits the form to the href location.( spool.asp)
2. Add the following code in the spool.asp file
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=test.csv"
If this is followed, on clicking the Export Image, the javascript function is called, which submits the form. As the form is submitted to a page with a content-type that IE cannot interpret, if would pop out the File Save as Dialog.
ASKER
Thanks guys, but I still get the 'access denied' error message:
Here is the code I have:
Reports.asp
--------------
function CheckForReport( )
{
document.forms(0).action = "Export.asp"
document.forms(0).submit() ;
}
<a href="javascript:CheckForR eport();"> <img type="image" alt="Export" src="../images/export_off. gif">
Export.asp
-------------
'' NOTE: The csv file is already in the web server
option explicit
Response.Expires = -1500
Response.Buffer = true
dim Stream
dim Contents
dim strFileName
dim strFile
strFile = Session.SessionID & ".csv"
strFileName = Server.MapPath("." & "\" & "exports\" & strFile)
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & strFile
set Stream = server.CreateObject("ADODB .Stream")
Stream.Open
Stream.Type = 1
Response.CharSet = "UTF-8"
Stream.LoadFromFile(strFil eName)
Response.BinaryWrite(Strea m.Read)
Stream.Close
Set Stream = Nothing
Response.Flush()
Here is the code I have:
Reports.asp
--------------
function CheckForReport( )
{
document.forms(0).action = "Export.asp"
document.forms(0).submit()
}
<a href="javascript:CheckForR
Export.asp
-------------
'' NOTE: The csv file is already in the web server
option explicit
Response.Expires = -1500
Response.Buffer = true
dim Stream
dim Contents
dim strFileName
dim strFile
strFile = Session.SessionID & ".csv"
strFileName = Server.MapPath("." & "\" & "exports\" & strFile)
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & strFile
set Stream = server.CreateObject("ADODB
Stream.Open
Stream.Type = 1
Response.CharSet = "UTF-8"
Stream.LoadFromFile(strFil
Response.BinaryWrite(Strea
Stream.Close
Set Stream = Nothing
Response.Flush()
This Question is now classified abandoned as no comment has been added to this question in more than 21 days.
I will leave the following recommendation on this question to the moderators in the Cleanup topic area:
Accept: NeilT {http:#9742415}
Any objections should be posted here in the next 4 days. After that time, the question will be closed at the discretion of the moderator.
vnvk
EE Cleanup Volunteer
I will leave the following recommendation on this question to the moderators in the Cleanup topic area:
Accept: NeilT {http:#9742415}
Any objections should be posted here in the next 4 days. After that time, the question will be closed at the discretion of the moderator.
vnvk
EE Cleanup Volunteer
Neil
I'm getting the Error As:
Error Type:
ADODB.Stream (0x800A0BBA)
File could not be opened.
/ASPProject/MaintenanceOld .asp, line 29
When i implemented your code in my asp page. Do i need to create test.csv in the folder where my asp page resides?
The code in my asp page looks like:
Dim objConn ' Connection Object
Dim objCmd ' Command Object
Dim objRS ' Recordset Object
Dim strSQL ' SQL String to access the database
Dim strConnection ' Connection string to access the database
Dim i ' Counter variable
Dim fs
Dim Stream
Dim Contents
Dim FileName
FileName = "test.csv"
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=test.csv"
Set Stream = server.CreateObject("ADODB .Stream")
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
Contents = Stream.ReadText
Response.BinaryWrite Contents
Stream.Close
Set Stream = Nothing
' -- Create objects
Set objConn = Server.CreateObject("ADODB .Connectio n")
Set objCmd = Server.CreateObject("ADODB .Command")
' -- Connection String Value
strConnection="DSN=Northwi nd"
' -- Open the Connection
objConn.Open strConnection
' Execute the Stored Procedure
With objCmd
.ActiveConnection = strConnection
.CommandType = adCmdStoredProc
.Commandtext = "MaintenanceInfo"
.Parameters.Append objCmd.CreateParameter("@C ustomerNo" ,adVarChar ,adParamIn put,10,925 41)
End with
Set objRS = Server.CreateObject("ADODB .Recordset ")
Set objRS = objCmd.Execute
' -- Populate our Recordset with data
'set objRS = objConn.Execute (strSQL)
If (objRS.BOF and objRS.EOF) then
response.write "No records found"
response.end
End if
'------------------------- ---------- ---------- ---------- ---------- -----
' Begin HTML output
'------------------------- ---------- ---------- ---------- ---------- -----
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Maintenance Sample.Accesing Data from the Database</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1" CELLPADDING="1" CELLSPACING="1" WIDTH="100%" HEIGHT="30px" ALIGN="CENTER">
<%
' -- Output the Field Names as the first row in the table
Response.Write "<TR BGCOLOR=""#CCCCCC"">"
For i = 0 to objRS.Fields.Count - 1
Response.Write "<TH><FONT FACE=""ARIAL"" SIZE=""2"">" & objRS.Fields(i).Name & "</FONT></TH>"
Next
Response.write "</TR>"
' -- Now output the contents of the Recordset
'objRS.MoveFirst
Do While Not objRS.EOF
' -- output the contents
Response.Write "<TR>"
For i = 0 to objRS.Fields.Count - 1
Response.Write "<TD><FONT FACE=""ARIAL"" SIZE=""1"">" & objRS.Fields(i) & "</FONT></TD>"
Next
Response.write "</TR>"
' -- move to the next record
objRS.MoveNext
Loop
objRS.Close
set objRS = Nothing
objConn.Close
set objConn = Nothing
%>
</TABLE>
</BODY>
</HTML>
Neil Can you please let me know why am i getting this error..
Thanks
I'm getting the Error As:
Error Type:
ADODB.Stream (0x800A0BBA)
File could not be opened.
/ASPProject/MaintenanceOld
When i implemented your code in my asp page. Do i need to create test.csv in the folder where my asp page resides?
The code in my asp page looks like:
Dim objConn ' Connection Object
Dim objCmd ' Command Object
Dim objRS ' Recordset Object
Dim strSQL ' SQL String to access the database
Dim strConnection ' Connection string to access the database
Dim i ' Counter variable
Dim fs
Dim Stream
Dim Contents
Dim FileName
FileName = "test.csv"
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=test.csv"
Set Stream = server.CreateObject("ADODB
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
Contents = Stream.ReadText
Response.BinaryWrite Contents
Stream.Close
Set Stream = Nothing
' -- Create objects
Set objConn = Server.CreateObject("ADODB
Set objCmd = Server.CreateObject("ADODB
' -- Connection String Value
strConnection="DSN=Northwi
' -- Open the Connection
objConn.Open strConnection
' Execute the Stored Procedure
With objCmd
.ActiveConnection = strConnection
.CommandType = adCmdStoredProc
.Commandtext = "MaintenanceInfo"
.Parameters.Append objCmd.CreateParameter("@C
End with
Set objRS = Server.CreateObject("ADODB
Set objRS = objCmd.Execute
' -- Populate our Recordset with data
'set objRS = objConn.Execute (strSQL)
If (objRS.BOF and objRS.EOF) then
response.write "No records found"
response.end
End if
'-------------------------
' Begin HTML output
'-------------------------
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Maintenance Sample.Accesing Data from the Database</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1" CELLPADDING="1" CELLSPACING="1" WIDTH="100%" HEIGHT="30px" ALIGN="CENTER">
<%
' -- Output the Field Names as the first row in the table
Response.Write "<TR BGCOLOR=""#CCCCCC"">"
For i = 0 to objRS.Fields.Count - 1
Response.Write "<TH><FONT FACE=""ARIAL"" SIZE=""2"">" & objRS.Fields(i).Name & "</FONT></TH>"
Next
Response.write "</TR>"
' -- Now output the contents of the Recordset
'objRS.MoveFirst
Do While Not objRS.EOF
' -- output the contents
Response.Write "<TR>"
For i = 0 to objRS.Fields.Count - 1
Response.Write "<TD><FONT FACE=""ARIAL"" SIZE=""1"">" & objRS.Fields(i) & "</FONT></TD>"
Next
Response.write "</TR>"
' -- move to the next record
objRS.MoveNext
Loop
objRS.Close
set objRS = Nothing
objConn.Close
set objConn = Nothing
%>
</TABLE>
</BODY>
</HTML>
Neil Can you please let me know why am i getting this error..
Thanks
ASKER
It's working but I have another issue. I wanted to stay in the same page so instead of going to a spool.asp page, I reload the page by doing a submit to itself. The "file download" dialog box comes up and I am able to save the file anywhere I want. Now, the problem is, if I try to click on other buttons on my page such as a 'Print' or 'help' button, I get a javascript 'Access Denied' error message. Please help...