Home > database >  How to get data from BlazorInputfile
How to get data from BlazorInputfile

Time:10-12

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...
                    }
            }            
        }

CodePudding user response:

I had this problem too . You can get file bytes and send that to your Api then create file again there .

in your component

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();
            }
}

in your api

//file.content is aray of byte

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

CodePudding user response:

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

'''

  • Related