469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

Process a CSV File in ASP.

MN
Hello all -

I'm hoping that someone knows the answer to this as I have not done this in
ASP before.

Basically, I need to select a csv file from my desktop through the
"Browse.." button in my ASP page and upload the file and do further
interrogation and extract the data to put into a database. What I'm
struggling with is more the first parts with getting the file read properly
in the second page so I can read and insert the data. The data below
describes the file. Quite a simple format.

Customer ID - Additional values are player IDs
193948294, 293, 294, 296, 290, 292

If anyone can help, I would greatly appreciate it. Thanks for reading.
Mark

May 23 '06 #1
3 8005
I use an script from Persists ASP Upload to get the file properly to
the right location but here is what i do for that reason. It is a free
download

Upload Form
<html>
<head><title>Upload Log Hours Form</title></head>
<body style="font-family: Arial">
<h3 class=View>Upload CSV File</h3>
<TABLE BORDER=0 WIDTH=70% CELLSPACING=0 COLSPAN=10>
<FORM ACTION="uploadfile.asp" METHOD="POST"
ENCTYPE="multipart/form-data">
<tr>
<td class=view><b>File Name</td><td class=view><input type=file
name="fname" size="36"></td>
<tr>
<tr><td class=view><input type=submit name=submit value=Upload></td><td
class=view><input type=reset name=submit value=Reset></td>
</form>
</table>
</body>
</html>
Second Page

<HTML>
<head><title>IMPORT</title></head>
<BODY>
<%

Set Upload = Server.CreateObject("Persits.Upload.1") 'Persists ASP
Upload Object
Upload.OverwriteFiles = False
Count = Upload.Save(Server.MapPath("\upload\")) 'Path to upload To
Dim faname
Dim i

faname(i) = File.Path
i = i +1
Next
Dim aVars(8) 'Amount of columns you project per line, if this is
dynamice per line use a REDIM Preserve on your FOR statement to REDIM
your array
Dim oStream
Dim sData
Dim aData
Dim oFSO
Set oFSO = CreateObject( "Scripting.FileSystemobject" )
Set oStream = oFSO.OpenTextFile( faname )
Do While oStream.AtEndOfStream = False 'Will Walk Through all the
records
sData = oStream.ReadLine
aData = Split( sData, "," )
newrec = newrec + 1
For i = 0 To ( UBound( aData ) )
aVars(i) = aData(i)
aVars(i) = TRIM(aVars(i)) ' Removes and blank spaces before and
after the Data
Next
'..
'.. Write To your database
'.. aVars(0) will be your first
Loop
%>
</BODY>
</HTML>

Ok here's how the second page works,
The file is downloaded to a specific location that you specify
Then your file is opened and read line by line until it is finished.
Every Line it reads it splits the data into an array for you to use
later as aVars(0), aVars(1) and so on.
Then the each Variable in the array is read and TRIM'd to remove any
spaces that may cause DB problems.
After that you can write it to your database using which ever type you
would like.
Let me know if this works out for you

May 23 '06 #2
Sorry that was really bad code

<html>
<head><title>Upload CSV</title></head>
<body style="font-family: Arial">
<h3 class=View>Upload CSV File</h3>
<TABLE BORDER=0 WIDTH=70% CELLSPACING=0 COLSPAN=10>
<FORM ACTION="uploadfile.asp" METHOD="POST"
ENCTYPE="multipart/form-data">
<tr>
<td class=view><b>File Name</td><td class=view><input type=file
name="fname" size="36"></td>
<tr>
<tr><td class=view><input type=submit name=submit value=Upload></td><td

class=view><input type=reset name=submit value=Reset></td>
</form>
</table>
</body>
</html>
Second Page
<HTML>
<head><title>IMPORT</title></head>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload.1") 'Persists ASP
Upload Object
Upload.OverwriteFiles = False
Count = Upload.Save(Server.MapPath("\upload\")) 'Path to upload To
Dim faname
faname = File.Path
Dim aVars(8) 'Amount of columns you project per line, if this is
dynamice per line use a REDIM Preserve on your FOR statement to REDIM
your array
Dim oStream
Dim sData
Dim aData
Dim oFSO
Set oFSO = CreateObject( "Scripting.FileSystemobject" )
Set oStream = oFSO.OpenTextFile( faname )
Do While oStream.AtEndOfStream = False 'Will Walk Through all the
records
sData = oStream.ReadLine
aData = Split( sData, "," )
newrec = newrec + 1
For i = 0 To ( UBound( aData ) )
aVars(i) = aData(i)
aVars(i) = TRIM(aVars(i)) ' Removes and blank
spaces before and
after the Data
Next
'..
'.. Write To your database
'.. aVars(0) will be your first
Loop
%>
</BODY>
</HTML>

May 23 '06 #3
MN
Nico -

Millions of thanks. Worked perfect.

Mark

"Nico VanHaaster" wrote:
Sorry that was really bad code

<html>
<head><title>Upload CSV</title></head>
<body style="font-family: Arial">
<h3 class=View>Upload CSV File</h3>
<TABLE BORDER=0 WIDTH=70% CELLSPACING=0 COLSPAN=10>
<FORM ACTION="uploadfile.asp" METHOD="POST"
ENCTYPE="multipart/form-data">
<tr>
<td class=view><b>File Name</td><td class=view><input type=file
name="fname" size="36"></td>
<tr>
<tr><td class=view><input type=submit name=submit value=Upload></td><td

class=view><input type=reset name=submit value=Reset></td>
</form>
</table>
</body>
</html>
Second Page
<HTML>
<head><title>IMPORT</title></head>
<BODY>
<%
Set Upload = Server.CreateObject("Persits.Upload.1") 'Persists ASP
Upload Object
Upload.OverwriteFiles = False
Count = Upload.Save(Server.MapPath("\upload\")) 'Path to upload To
Dim faname
faname = File.Path
Dim aVars(8) 'Amount of columns you project per line, if this is
dynamice per line use a REDIM Preserve on your FOR statement to REDIM
your array
Dim oStream
Dim sData
Dim aData
Dim oFSO
Set oFSO = CreateObject( "Scripting.FileSystemobject" )
Set oStream = oFSO.OpenTextFile( faname )
Do While oStream.AtEndOfStream = False 'Will Walk Through all the
records
sData = oStream.ReadLine
aData = Split( sData, "," )
newrec = newrec + 1
For i = 0 To ( UBound( aData ) )
aVars(i) = aData(i)
aVars(i) = TRIM(aVars(i)) ' Removes and blank
spaces before and
after the Data
Next
'..
'.. Write To your database
'.. aVars(0) will be your first
Loop
%>
</BODY>
</HTML>

May 29 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Justin Johnson | last post: by
1 post views Thread by Peter Åstrand | last post: by
11 posts views Thread by Mike M | last post: by
reply views Thread by Markus Poehler | last post: by
10 posts views Thread by Sorin Dolha [MCSD .NET] | last post: by
12 posts views Thread by Raymond Lewallen | last post: by
4 posts views Thread by Paddy | last post: by
7 posts views Thread by jonathandrott | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.