0

I am uploading a file into a controller using the following -

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);

            //I want to put file contents into a string or List<string>

        }
    }

I would like to either put the contents of the file into a string then loop through the string which will be a delimited list,

or

loop through the incoming stream itself, creating a list of strings out of it. I can't figure out how to do either. I assume I would use file.InputStream in some manner? Any help would be appreciated. Thanks!

1
  • If you know that your're file will be a text file why don't you use System.IO.File.ReadAllText ? Or maybe i'm not getting it Commented Apr 2, 2013 at 21:00

1 Answer 1

1

Try using StreamReader, something like this:

string s = (new StreamReader(file.InputStream)).ReadToEnd();
string[] ss = s.Split(","); // replace "," with your separator;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.