如何从 BlazorInputfile 获取数据

How to get data from BlazorInputfile

我在我的项目中使用 BlazorInputFile 但不知道如何将从输入文件(一个 zipFile)获得的流转换为 ZipArchive 以在其中循环....

我看到流没问题,但是当我尝试将 copytoasync 复制到内存流时,它不起作用,告诉我变量不可用。

所以我尝试在 copytoasync 之前等待一个异步任务而不是我的 void 函数 loadFile,我现在看到 ms 可用但它是空的,大小为 0...似乎在 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...
                    }
            }            
        }

我也有这个问题。 您可以获得文件字节并将其发送到您的 Api 然后在那里再次创建文件。

在你的组件中

public byte[] FileContent { get; set; }

protected async Task HandleAnswerFileSelected(IFileListEntry[] files)
    {
        foreach (var file in files)
        {
             FileContent = await FIleSender(file),
            
        }
}

public async Task<byte[]> FIleSender(IFileListEntry file)
{
            using (var ms = new MemoryStream())
            {
                await file.Data.CopyToAsync(ms);
                return ms.ToArray();
            }
}

在你的 api

//file.content is aray of byte

using var memoryStream = new MemoryStream(file.Content);

我找到了解决方案并使其像这样工作,将其转换为 base64string 并将其写入新的 ms! 也许它可以帮助。

'''

      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)
                {
                }
        }
    }

'''