OffBeatMammal

Searching for monkeys in Cyberspace

Getting access to an ASP.NET uploaded file

clock November 16, 2005 23:59 by author offbeatmammal

Sometimes in ASP.NET you just want to get access to the content of a text file uploaded from a form. I wanted to use it in conjunction with my nifty RegEx CSV Processor but... here it is to share

1:  <%
2:  ' upload a text file (eg a csv) and make the content available as an array of lines  
3:  ' Hybrid ASP.NET (VB) Solution  
4:  Dim fileFld
5:  Dim byteLoop, fileText
6:  
7:  If lcase(Request.ServerVariables("REQUEST_METHOD"))="post" Then
8:     fileFld = request.files(0)
9:     If fileFld.filename = "" Then
10:          ' no file to process  
11:     Else         12:         Dim intFileLen As Integer = fileFld.ContentLength ' find out how long the file is
13:         Dim b(intFileLen) As Byte ' and create an array big enough to hold it
14:          ' move the uploaded file into the byte array  
15:         fileFld.InputStream.Read(b, 0, intFileLen)
16:           ' Copy the byte array into a string.  
17:          For byteLoop = 0 To intFileLen-1
18:             fileText = fileText & Chr(b(byteLoop))
19:          Next byteLoop
20:          ' split the file into an array  
21:         fileLine = Split(fileText,Chr(13))
22:     End If
23:  End If
24:  %>
25:  
26:  .....
27:  
28:  <form enctype="multipart/form-data" method="post" runat="server">
29:  <table>
30:  <tr><td>Select the file to upload:</td><td><input type="file" name="upload_file"></td></tr>
31:  <tr><td colspan="2"><input type="submit" value="Upload">
32:  </table>
33:  </form>
34:  %>


Using a RegEx (Regular Expression) to read a CSV (Comma Seperated Variable) file

clock November 16, 2005 23:50 by author offbeatmammal
Had to solve one of those tricky little problems today... a user uploading a CSV and splitting the fields into a database.

Played around with the ADO CSV driver but... it's very picky about the quality of the data and ended up being harder work than a simple CSV import function should be...

A quick search (Google is easier than thinking after all) revealed a bunch of regex to handle csv input... the only trouble being that most of the solutions where partial...

So... here's my contribution... handles CSVs and copes with quoted items.

1:  <%
2:  ' Split a line of a CSV into array cells  
3:  ' support quoted (") entries with internal commas  
4:  ' Hybrid ASP.NET (VB) Solution  
5:  
6:  dim csvRegEx As Regex = New RegEx(",(?=(?:[^\""]*\""[^\""]*\"")*(?![^\""]*\""))")
7:  dim source_str
8:  dim csvArray, loopArray
9:  
10:  ' load source_str - this can be from a text file, or an uploaded file  
11:  
12:  csvArray = csvRegEx.Split(source_str)
13: ' Strip off quotes (if the entry was quoted)
14:  For loopArray = 0 To UBound(csvArray)
15:     if csvArray(loopArray) <> "" Then
16:         If Left(csvArray(loopArray),1) = Chr(34) And Right(csvArray(loopArray),1) = Chr(34) Then
17:             csvArray(loopArray) = trim(Mid(csvArray(loopArray),2,Len(csvArray(loopArray))-2))
18:         End if
19:     End if
20:  Next
21:  %>


Search

Calendar

<<  May 2013  >>
SuMoTuWeThFrSa
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Sign in

Twitter


    follow OffBeatMammal at http://twitter.com


    Amazon Store


     
    Donate unused CPU cycles with BOINC Stats and Account Management from BOINCStats.com



    Blogroll

    Archive

    Tags

    Categories


    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2013