PDA

View Full Version : Data from Database to a CSV file download


Dr.WiggY
08-11-2003, 02:36 AM
hmm....

this one is tricky, i have no problems collecting the data but does anyone know how i would go about saving all the information and then give someone the option of downloading that data to a CSV file to where they want ?

Would the information somehow need to be stored in a cookie array ? (if thats possible)
to then be called and looped into a file download.

whammy
08-11-2003, 02:53 AM
Actually this question has already been answered twice by me personally on this forum... and perhaps answered more times... I shall see if I can find the appropriate threads...

whammy
08-11-2003, 02:59 AM
Ok... this is a working example of a form that posts information and automatically appends it to a .csv (text) file.


<%@ Language="VBScript" %>
<% Option Explicit %>
<%
Dim fs, f, thefile, linevalue, LeadColumns
Dim fieldName, fieldValue
Dim i, NewFile

NewFile = False

If Request.Form.Count = 4 Then
Call ProcessForm()
Call DisplayThankYou()
Else
Call DisplayForm()
End If

Sub ProcessForm()

thefile = Server.MapPath("vet.csv")

Set fs = Server.CreateObject("Scripting.FileSystemObject")

Set f = fs.OpenTextFile(thefile, 1, True)
If f.AtEndOfStream Then NewFile = True
f.Close

If NewFile = True Then
Set f = fs.OpenTextFile(thefile, 2)
For i = 1 to Request.Form.Count
fieldName = Request.Form.Key(i)
fieldValue = Request.Form.Item(i)
LeadColumns = LeadColumns & """" & Replace(fieldName,"""","") & ""","
Next
LeadColumns = Left(LeadColumns,Len(LeadColumns)-1)
f.WriteLine(LeadColumns)
f.Close
End If

Set f = fs.OpenTextFile(thefile, 8)
For i = 1 to Request.Form.Count
fieldName = Request.Form.Key(i)
fieldValue = Request.Form.Item(i)
linevalue = linevalue & """" & Replace(fieldValue,"""","") & ""","
Next
linevalue = Left(linevalue,Len(linevalue)-1)
f.WriteLine(linevalue)
f.Close

Set f = Nothing
Set fs = Nothing
End Sub
%>

<% Sub DisplayForm() %><html>
<head>
<title>.: Tell us what you think! :.</title>
<style type="text/css">
<!--
body
{
background-color:#99cc99;
}
table
{
margin-left:auto;
margin-right:auto
}
td
{
font-family:arial,helvetica;
font-size:10pt
}
.cf
{
font-family:courier new;
font-size:10pt;
width:200px;
background-color:#ccffcc
}
-->
</style>
<script type="text/javascript">
<!--
if (!document.all && !document.getElementById)
{
alert("Your browser is obsolete. Please install a modern browser.");
location = "http://channels.netscape.com/ns/browsers/download.jsp";
}

function trim(str)
{
return str.replace(/^\s+/,"").replace(/\s{2,}/g," ").replace(/\s+$/,"");
}

function validateForm(f)
{
/* Declare variables */

var errorMessage = "";
var focusField;
var emailRegex = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

/*
* Trim leading and trailing spaces from entries -
* add more element types as needed
*/

for (var i = 0; i < f.elements.length; i ++)
{
var x = f.elements[i];
if (x.type == "text" || x.type == "textarea")
{
x.value = trim(x.value);
}
}

/* Validate required fields */

if (f.Name.value == "" ||
f.Name.value == f.Name.defaultValue)
{
errorMessage += "- Name is required\n";
f.Name.value = f.Name.defaultValue;
if (!focusField) focusField = "Name";
}

if (!emailRegex.test(f.Email.value) ||
f.Email.value == f.Email.defaultValue)
{
errorMessage += "- Invalid email address\n";
f.Email.value = f.Email.defaultValue;
if (!focusField) focusField = "Email";
}

if (f.Subject.value == "" ||
f.Subject.value == f.Subject.defaultValue)
{
errorMessage += "- Subject is required\n";
f.Subject.value = f.Subject.defaultValue;
if (!focusField) focusField = "Subject";
}

if (f.Message.value == "" ||
f.Message.value == f.Message.defaultValue)
{
errorMessage += "- Message is required\n";
f.Message.value = f.Message.defaultValue;
if (!focusField) focusField = "Message";
}

/* Perform final validation */

if (errorMessage != "")
{
alert("Please correct the following errors:\n\n" + errorMessage);
eval("f." + focusField + ".focus()");
eval("f." + focusField + ".select()");
return false;
}
else
{
return true;
}
}
// -->
</script>
</head>
<body>
<div>
<form id="form1" style="text-align:center" action="csv.asp" method="post" onsubmit="return validateForm(this)">
<table>
<tr>
<td valign="top">
<input type="text" name="Name" value="YOUR NAME" class="cf" onfocus="this.select()" onblur="this.value=trim(this.value)" />
</td>
</tr>
<tr>
<td valign="top">
<input type="text" name="Email" value="YOUR EMAIL ADDRESS" class="cf" onfocus="this.select()" onblur="this.value=trim(this.value)" />
</td>
</tr>
<tr>
<td valign="top">
<input type="text" name="Subject" value="YOUR SUBJECT" class="cf" onfocus="this.select()" onblur="this.value=trim(this.value)" />
</td>
</tr>
<tr>
<td valign="top">
<textarea rows="7" cols="45" name="Message" class="cf" style="overflow:auto" onfocus="this.select()" onblur="this.value=trim(this.value)">YOUR MESSAGE</textarea>
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value="Send Message" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html><% End Sub %>

<% Sub DisplayThankYou %><html>
<head>
<title>.: Tell us what you think! :.</title>
<style type="text/css">
<!--
body
{
background-color:#99cc99;
}
table
{
margin-left:auto;
margin-right:auto
}
td
{
font-family:arial,helvetica;
font-size:10pt
}
.cf
{
font-family:courier new;
font-size:10pt;
width:200px;
background-color:#ccffcc
}
-->
</style>
</head>
<body>
<div>
<span style="font-weight:bold">Thank You!</span>
</div>
<script type="text/javascript">
<!--
setTimeout("location='csv.asp'",3000);
// -->
</script>
</body>
</html><% End Sub %>


You should be able to modify that to work after/with writing to a database, or to only write to the .csv file in a subroutine, once you see how it works. Hope it helps. :D

Dr.WiggY
08-11-2003, 05:48 AM
Originally posted by whammy
Actually this question has already been answered twice by me personally on this forum...

along with all the other repeated threads hehe :p :p

whammy
08-11-2003, 06:05 AM
I couldn't find the threads... but this should help

;)

whammy
08-11-2003, 06:07 AM
If you need more help, this is a pretty well-rounded forum for classic ASP. :)

P.S. That example script can be optimized much further (example: store the javascript that is repeated on the client-side as an external javascript library - to start with). :)

Dr.WiggY
08-12-2003, 01:29 AM
thanks for lookin :)
and
i spose we could keep this discussion going if needed
but i have managed to finish what i needed to do. so thanks for the help. anyone else wants in on how i did it reply back.

(my idea was for purpose of saving the file onto users local machine not onto the server.)

whammy
08-12-2003, 02:43 AM
In that case, I'd make an educated guess that you used cookies. ;) LOL.

If not you used ActiveX, but that's much less reliable...

Dr.WiggY
08-13-2003, 12:58 AM
C is for Cookie

YUMMMMMMM! ;)

glenngv
08-13-2003, 04:15 AM
No need for cookies or ActiveX...

<%
response.buffer = true
response.contenttype="application/csv"
'loop thru the recordset to generated comma separated values
'sample output below
%>
row1-column1,row1-column2,row1-column3,row1-column4
row2-column1,row2-column2,row2-column3,row2-column4
row3-column1,row3-column2,row3-column3,row3-column4


That will cause the browser to display the File Open-Save dialog.

whammy
08-13-2003, 04:29 AM
That's cool, I usually work off of the assumption that .csv files either automatically open in excel (or another database or text program), or users are prompted to save it... so I just usually generate it on the fly with FSO and give them a link. ;o)

That's another nice alternative though.

glenngv
08-13-2003, 04:48 AM
If you choose Open, the file will be opened in Notepad.
If Save, you have to instruct the users to save the file with the .csv extension. The default filename in the Save As dialog is the filename of the current page which is an .asp file.

I've made a little change in the code because the data start at the 2nd row not the 1st row. So I moved the data at the start of the page:

row1-column1,row1-column2,row1-column3,row1-column4
row2-column1,row2-column2,row2-column3,row2-column4
row3-column1,row3-column2,row3-column3,row3-column4
<%
response.buffer = true
response.contenttype="application/csv"
%>