Read PostedFile Directly into a String Variable

Posted on 2004-11-01
Last Modified: 2012-05-05

I'm uploading a file through a form using a File server control.

The file uploads fine into memory, and I can save the file to disk using a line of code such as "MyFile.PostedFile.SaveAs...."

What I really want to do is that this entire file, and get it into a String variable (without having to save it to disk at all).  The contents of the file are just plain text with line breaks (no null characters or anything).  I'm pretty sure I'd use the MyFile.PostedFile.InputStream.Read method, but it looks like this method returns an integer, or perhaps an array of bytes.

So, I guess my question is..  How do I get the entire contents of a file uploaded through a File server control into a String variable, assuming the file contains no special characters other than line breaks.  I do NOT want to save the file to a disk first and then re-read it through an StreamReader object.

Thanks in advance,

Question by:dsulli2000
    1 Comment
    LVL 96

    Accepted Solution

    You could pass that stream to a StreamReader and either read it one line at a time (ReadLine) or all at once (ReadAll).  One of the constructors for a StreamReader takes a stream as the parameter, so you wouldn't have to save to disk first.  

    The Read method returns the number of bytes read into the byte array buffer.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now