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

Thread is closed for posting
2 posts, 0 answers
  1. Kevin
    Kevin avatar
    30 posts
    Member since:
    Sep 2012

    Posted 05 Jul 2012 Link to this post

    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?
  2. Kevin
    Kevin avatar
    30 posts
    Member since:
    Sep 2012

    Posted 05 Jul 2012 Link to this post

    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.

Back to Top