Print Page | Close Window

db to .csv and download

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=23227
Printed Date: 29 March 2026 at 10:26am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: db to .csv and download
Posted By: Dan H
Subject: db to .csv and download
Date Posted: 01 May 2007 at 2:18am
I'd like to create (on demand) a .csv file from records in my db, then download the file to the client.
I think it would go like this:
(collect variables in asp, then)

set fs=server.createobject("scripting.filesystemobject")
set f=fs.createtextfile(server.mappath("/test.csv"),true)
f.writeline("<%=value1%>" & "," & "<%=value2%>")
f.writeline("<%=value3%>" & "," & "<%=value4%>")
f.close
<a href="http://www.mysite.com/test,csv">Download</a>

Does this make any sense?
Is there a cleaner way to do it?
I should probably create a unique name for the file to keep somebody from creating the file, then letting the link sit on their browser for awhile- they could get someone elses file.
I'd have to delete it after download.
How would I do that?
Thanks for any thoughts.
Dan




Replies:
Posted By: kbannon
Date Posted: 01 May 2007 at 12:15pm
You are writing one record on two lines - is that intentional?

After you have:
set fs=server.createobject("scripting.filesystemobject")
set f=fs.createtextfile(server.mappath("/test.csv"),true)
you should set up a recordset and loop through the records with each iteration of the loop writing to the csv file.
After the loop has finished you close the recordset & connection and then close 'f'
To save the file with a unique name just include the date in the filename?

See http://www.webdeveloper.com/forum/archive/index.php/t-34898.html  for a bit more.


-------------
I don't suffer from insanity, I enjoy every minute of it.


Posted By: Dan H
Date Posted: 01 May 2007 at 2:07pm
Ok- that's what I was thinking.
Thanks.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net