I'm sent large files from an API as base64 encoded strings which I convert to a byte[]
array (in one go) and then return to the client via controller action, example:
byte[] fileBytes = Convert.FromBase64String(base64File);
return this.File(fileBytes);
In some cases, these files end up being very large which I believe is causing the fileBytes
object to be allocated to the LOH where it wont be immediately freed once out of scope. This is happening enough that it is causing out of memory exceptions and the application to restart.
My question is, how can I read these large base64 strings without allocating a byte[]
to the LOH? I thought about reading it into a stream and then returning a FileStreamResult instead e.g:
using(var ms = new MemoryStream(fileBytes))
{
// return stream from action
}
But I'd still need to convert the base64 to byte[]
first. Is it possible to read the the base64 itself in smaller chunks, therefore creating smaller byte[]
which wouldn't end up in LOH?