Trang Bartell

Trang Bartell

  • 1.7k
  • 52
  • 1.4k

MVC - Print contents of files (DOCX, PDF, etc) stored on SQL SERVER

May 15 2024 1:46 PM

I'm working on a MVC 5 application and have a need to print a report listing the contents of each files stored on SQL SERVER database.  The files are of various types (DOC, DOCX, PDF..).  The datatype used is VARBINARY(MAX).  My codes below worked to return 1 document at at time, but what I need is to have a loop and print out the contents of each document one after the other.

I'm hoping to be able to do the following.  Is it possible? Please help.  Thank you.

if (Model.Case_Documents_List.Count > 0)
{
    <table class="table table-bordered table-responsive table-hover" style="font-size: large; font-family: Arial">
        @foreach (var c in Model.Case_Documents_List)
        {
            <tr>
                @c.DOC_DOCUMENT_Content
            </tr>
        }
    </table>
}

 

The codes below worked to download 1 document at a time:

try
{
    byte[] FileBytes = System.IO.File.ReadAllBytes(thePath);

    if (FileBytes != null)
    {
        if (FileExt == "PDF")
            return File(FileBytes, "application/pdf");

        else if (FileExt == "JPG" || FileExt == "PNG")
            return File(FileBytes, "image/jpeg");

        else if (FileExt == "TXT")
            return File(FileBytes, "text/plain");

        else if (FileExt == "XLS" || FileExt == "XLSX")
            return File(FileBytes, "application/vnd.ms - excel");

        else if (FileExt == "PPT")
            return File(FileBytes, "application/vnd.ms-powerpoint");

        else if (FileExt == "GIF")
            return File(FileBytes, "image/gif");

        else if (FileExt == "TIF")
            return File(FileBytes, "image/tiff");

        else if (FileExt == "RTF")
            return File(FileBytes, "application/rtf");

}


Answers (1)