0

i want to copy the data of an uploaded file to an IList object. The uploaded file is .csv-data.

My code looks like this:

        public void FileToIList(IFormFile importFile)
        {
            if (importFile != null)
            {
                //var path = Path.Combine(wwwrootDiectory, DateTime.Now.Ticks.ToString() + Path.GetExtension(importFile.FileName));
                //IList<string> = importFile.OpenReadStream().Read() <= does not work
        }

But I don't want to save the content of the importFile in a temporary data. I want to write the content in directly way into a IList object. Is it possible?

After this lines I don't know further. Do you have a solution or maybe a idea?

Christian01
  • 307
  • 1
  • 5
  • 19

1 Answers1

0

If you have the file name, you can use File.ReadAllLines() to get an array (not a list) of all the lines in the file.

If you really have to have a list, or you only have access to a stream, you can use the method in this answer.

gnud
  • 77,584
  • 5
  • 64
  • 78
  • hey @gnud, thank you for your response. I have no filepath, because i get the data from MVC-View and want to store it in a IList to work futher with the data-content. I want to avoid to store the data in a server directory. – Christian01 Oct 22 '21 at 07:46
  • Did you try the approach in the other answer I linked? There's a simple method there to read a stream once line-by-line and yield each line into an enumerable. It should be easy to append to a list instead. – gnud Oct 22 '21 at 13:05