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: %>
|