Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert IFormfile to string

Tags:

c#

.net-core

zip

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");
       }
    }
}
like image 726
Novica Josifovski Avatar asked Nov 15 '25 13:11

Novica Josifovski


2 Answers

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());
        }
    }
like image 128
Steve Smith Avatar answered Nov 17 '25 01:11

Steve Smith


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.

like image 32
bolkay Avatar answered Nov 17 '25 02:11

bolkay