Search code examples
c#asp.net.net-coreblazormemorystream

How to get data from BlazorInputfile


i use BlazorInputFile on my project but dont know how to transform the stream that i get from the input file(a zipFile) to an ZipArchive to loop in it....

i see the stream is ok but when i try to make an copytoasync to a memorystream it dont work telling me the variable is not available.

So i try with an await befor the copytoasync with a async task instead of my void function loadFile, and i saw now the ms available but its empty, size is 0... seems nothing happened in the copytoasync...

private async Task loadFileAsync(IFileListEntry fileZip, ExcelWorksheet sheet2User)
        {
              MemoryStream mstest = new MemoryStream();
              await fileZip.Data.CopyToAsync(mstest);
              mstest.Position = 0;

            using (ZipArchive archive = new ZipArchive(mstest, ZipArchiveMode.Update))
            {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        //my code...
                    }
            }            
        }

Solution

  • i find the solution and make it work like this, converting it to base64string and write it to a new ms! Maybe it can help.

    '''

          private void LoadFile(MemoryStream msFromBlazorInputFile)
        {
            MemoryStream memoryfilestream = new MemoryStream(0);
            memoryfilestream.Write(Convert.FromBase64String(Convert.ToBase64String(msFromBlazorInputFile.ToArray())));
    
            using (ZipArchive archive = new ZipArchive(memoryfilestream, ZipArchiveMode.Update))
            {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                    }
            }
        }
    

    '''