The "AAAA..." pattern indicates you're getting null bytes in your buffer. The issue is that ReadAsync(buffer)
doesn't guarantee reading the entire stream in one call.
Use CopyToAsync()
with a MemoryStream instead:
using var stream = file.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
using var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream);
var base64String = Convert.ToBase64String(memoryStream.ToArray());
For a complete solution with security considerations, check out this guide: How to Convert an Image to a Base64 String in Blazor