A Bogus
A Bogus

Reputation: 3930

MVC C# application, put content of file into string or List<string>

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!

Upvotes: 0

Views: 654

Answers (1)

Floremin
Floremin

Reputation: 4089

Try using StreamReader, something like this:

string s = (new StreamReader(file.InputStream)).ReadToEnd();
string[] ss = s.Split(","); // replace "," with your separator;

Upvotes: 1

Related Questions