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...
}
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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);
Method 2
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)
{
}
}
}
”’
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0