Novica Josifovski
Novica Josifovski

Reputation: 65

Cannot convert IFormfile to string

I am trying to read a zip file with I get via IFormFile and I am going through the files inside to unzip a selected extension and unzip it but I cannot use the IFormFile to read it. It says cannot convert IFormFile to string.

Any suggestions on how to tackle this?

using (ZipArchive archive = ZipFile.OpenRead(file))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
       if (entry.FullName.EndsWith(".dbf", StringComparison.OrdinalIgnoreCase))
       {
         RedirectToAction("Index");
       }
    }
}

Upvotes: 3

Views: 3696

Answers (2)

Steve Smith
Steve Smith

Reputation: 2270

Although this won't help in this particular case (since ZipFile.OpenRead() expects a file), for those coming here after googling "iformfile to string", you can read an IFormFile into a string, for example as follows:-

    var result = new StringBuilder();
    using (var reader = new StreamReader(iFormFile.OpenReadStream()))
    {
        while (reader.Peek() >= 0)
        {
            result.AppendLine(reader.ReadLine());
        }
    }

Upvotes: 1

bolkay
bolkay

Reputation: 1909

Because IFormFile != string. I guess OpenRead expects a file path.

So, first read the content of the IFormFile into a file, then use that file path.

Upvotes: 1

Related Questions