Process a CSV File in ASP.

M

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
 
N

Nico VanHaaster

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
 
N

Nico VanHaaster

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top