Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 23rd, 2006, 07:25 PM
MN
Guest
 
Posts: n/a
Default Process a CSV File in ASP.

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

  #2  
Old May 23rd, 2006, 11:35 PM
Nico VanHaaster
Guest
 
Posts: n/a
Default Re: Process a CSV File in ASP.

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

  #3  
Old May 23rd, 2006, 11:45 PM
Nico VanHaaster
Guest
 
Posts: n/a
Default Re: Process a CSV File in ASP.

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>

  #4  
Old May 29th, 2006, 09:15 PM
MN
Guest
 
Posts: n/a
Default Re: Process a CSV File in ASP.

Nico -

Millions of thanks. Worked perfect.

Mark

"Nico VanHaaster" wrote:
[color=blue]
> 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>
>
>[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.