We store PDFs in our database in a ReportFiles
table which has a FileContents
column with the file data encoded as Base64. These are served from ASP.NET Core by converting the FileContents
(which is a string
) to a byte[]
and returning it using File()
. Lately we have experienced issues with requests timing out with large files while converting them from Base64. It is simply taking too long to convert them to bytes. Here is an example of the function:
public async Task<IActionResult> DownloadReport(int id)
{
ReportFile file = await context.ReportFiles.FindAsync(id);
if (file == null)
{
return NotFound();
}
byte[] fileBytes = Convert.FromBase64String(file.FileContents);
ContentDisposition cd = new ContentDisposition
{
FileName = file.FileName,
Inline = true
};
Response.Headers[HeaderNames.ContentDisposition] = cd.ToString();
return File(fileBytes, "application/pdf");
}
Is there a way to either serve the file directly as Base64 without converting it, or possibly converting it in chunks as the file downloads, rather than all at once before serving it to the client?