title: Read large base64 strings into chunks to avoid allocation to LOH C#
body: <p>I'm sent large files from an API as base64 encoded strings which I convert to a <code>byte[]</code> array (in one go) and then return to the client via controller action, example:</p>
  <pre><code>byte[] fileBytes = Convert.FromBase64String(base64File);
  
  return this.File(fileBytes);
  </code></pre>
  <p><a href="https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.filecontentresult.-ctor?view=aspnet-mvc-5.2#system-web-mvc-filecontentresult-ctor(system-byte()-system-string)" rel="nofollow noreferrer">FileContentResult - MSDN</a></p>
  <p>In some cases, these files end up being very large which I believe is causing the <code>fileBytes</code> 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.</p>
  <p>My question is, how can I read these large base64 strings without allocating a <code>byte[]</code> to the LOH? I thought about reading it into a stream and then returning a <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filestreamresult?view=aspnetcore-6.0" rel="nofollow noreferrer">FileStreamResult</a> instead e.g:</p>
  <pre><code>using(var ms = new MemoryStream(fileBytes))
  {
      // return stream from action
  }
  </code></pre>
  <p>But I'd still need to convert the base64 to <code>byte[]</code> first. Is it possible to read the the base64 itself in smaller chunks, therefore creating smaller <code>byte[]</code> which wouldn't end up in LOH?</p>