0

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?

Valuator
  • 3,262
  • 2
  • 29
  • 53
  • 1
    You can try to extend the time the server has to process the large file. https://stackoverflow.com/questions/37474309/timeouts-with-long-running-asp-net-mvc-core-controller-httppost-method – Jeff Jun 24 '20 at 21:49
  • 2
    The next idea would be to use javascript on the front end to convert the file after it's downloaded the base64 string – Jeff Jun 24 '20 at 21:51
  • You can also create/use a job system and put this process in the background and notify users when the job finishes. This architecure is much more responsive. – AminSojoudi Jun 24 '20 at 21:55
  • Open Task Manager when doing a large file conversion and check the memory being used. Either you have a memory issue or a slow (or bad) connection. To test connection I like using PING.-t -l 65500 IP where t indicates to send until manually stop and l is the length of each message. Using PING with large message give better indication of health of the connection. – jdweng Jun 24 '20 at 23:46
  • @Jeff at this point that may be my best option. – Valuator Jun 25 '20 at 15:13

0 Answers0