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)
{
}
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");
}
Vikas SinghPosted May 15, 2024, 4:20 PM
To achieve your goal of printing out the contents of each document stored in the SQL Server database, you can modify your approach to read each document's contents from the database and then display or download them one after the other. Since you're dealing with various file types stored as VARBINARY(MAX), you'll need to determine the appropriate way to handle each file type.
Here's how you can modify your code:
@if (Model.Case_Documents_List.Count > 0)
{
@foreach (var c in Model.Case_Documents_List)
{
@Html.ActionLink("View Document", "ViewDocument", "YourController", new { documentId = c.DocumentId }, null)
}
}
In the above code:
"YourController"with the name of your controller."ViewDocument"with the name of the action method in your controller that will handle displaying or downloading the document.documentIdshould be the parameter name you expect to receive in the action method.Now, in your controller, you need to implement the
ViewDocumentaction method:public ActionResult ViewDocument(int documentId)
{
// Retrieve the document from the database using the documentId
var document = YourDatabaseContext.Case_Documents.FirstOrDefault(d => d.DocumentId == documentId);
if (document == null)
{
// Document not found, handle error
return HttpNotFound();
}
// Convert VARBINARY data to byte array
byte[] fileBytes = document.DOC_DOCUMENT_Content;
// Determine the file extension
string fileExt = document.FileExtension.ToUpper();
// Set appropriate content type based on file extension
string contentType;
switch (fileExt)
{
case "PDF":
contentType = "application/pdf";
break;
case "JPG":
case "JPEG":
contentType = "image/jpeg";
break;
case "PNG":
contentType = "image/png";
break;
// Add cases for other file types as needed
default:
contentType = "application/octet-stream"; // Default to binary stream
break;
}
// Return the file as a FileResult
return File(fileBytes, contentType);
}
In the
ViewDocumentaction method:documentId.fileBytes) and its file extension (fileExt).contentType) based on the file extension.FileResultwith the specified content type.This setup should allow you to loop through your list of documents, and when a user clicks "View Document," it will display or download the document based on its file type.