This is a migrated thread and some comments may be shown as answers.

How can I read the CSV file the user just uploaded?

1 Answer 220 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 05 Jul 2012, 06:52 PM
I don't want to save a physical file anywhere.  The user needs to upload a CSV file and then I want to read it directly in memory and do things based off what I find in that file.

I found a sample on this site saying you could use the "InputStream" property to read in the file, but the example showed a simple table with "file name", "file size", and one big blob column for "file contents", which is not really reading the file itself.  I need to get the contents of the file into a string and then break that apart by commas and process accordingly.

Is there a way for me to take the uploaded file bytes and turn them into a string that contains the exact data the CSV file contains, or is a byte array the best I can do without writing the file to disk?

1 Answer, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 05 Jul 2012, 07:02 PM
As usual, I should have given myself 10 more minutes of googling before I posted my question.

Well, for anyone else wondering how to do this, here is what I discovered that works:

'Read the file into a byte array

 

Dim saleBytes(uploadedFiles(0).ContentLength) As Byte

uploadedFiles(0).InputStream.Read(saleBytes, 0, saleBytes.Length)


Dim
s As String = System.Text.Encoding.ASCII.GetString(saleBytes)

 
'Now "s" contains the exact contents of the CSV file, or whatever may have been uploaded

Edit: Sorry, when I click the "Format Code Block" button the WebSense filter at work blocks the resulting page.  Hopefully the code is readable without it.

Tags
Upload (Obsolete)
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Share this question
or