Files
DXApp/DXApp.TemplateKitProject/Pages/Invoices/ViewAttachment.cshtml.cs
OlgunR 8065c589bc Enhance file attachment viewing experience
Replaced direct file links with a JavaScript-based viewer
(`openAttachmentViewer`) and added a DevExtreme Popup
(`attachment-viewer-popup`) for displaying attachments in a
modal dialog. The viewer supports PDFs (via PDF.js), XML
(with CodeMirror syntax highlighting), plain text, images,
and provides a fallback for unsupported file types.

Dynamically load CodeMirror for XML files and handle errors
gracefully when loading file content. Added `onAttachmentPopupHiding`
to clear popup content on close.

Updated `ViewAttachmentModel` to return text/XML content
directly for AJAX requests, improving frontend performance
and enabling dynamic content loading.
2026-06-02 15:21:11 +02:00

50 lines
1.7 KiB
C#

using DXApp.TemplateKitProject.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text;
namespace DXApp.TemplateKitProject.Pages.Invoices;
public class ViewAttachmentModel(AttachmentViewerService viewerService) : PageModel
{
public string FileName { get; private set; } = string.Empty;
public AttachmentViewerType ViewerType { get; private set; }
public string? TextContent { get; private set; }
public IActionResult OnGet(string filePath)
{
if (string.IsNullOrEmpty(filePath) || !System.IO.File.Exists(filePath))
return NotFound();
FileName = Path.GetFileName(filePath);
ViewerType = viewerService.DetermineViewerType(FileName);
// Für Text/XML: Inhalt direkt als Content zurückgeben (für AJAX)
if (ViewerType == AttachmentViewerType.Xml || ViewerType == AttachmentViewerType.Text)
{
TextContent = System.IO.File.ReadAllText(filePath, Encoding.UTF8);
// Wenn Request von AJAX kommt (Accept: */* oder text/plain)
if (Request.Headers.Accept.ToString().Contains("*/*") ||
Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
return Content(TextContent, "text/plain", Encoding.UTF8);
}
}
return Page();
}
public IActionResult OnGetDownload(string filePath)
{
if (string.IsNullOrEmpty(filePath) || !System.IO.File.Exists(filePath))
return NotFound();
var fileName = Path.GetFileName(filePath);
var mimeType = viewerService.GetMimeType(fileName);
var bytes = System.IO.File.ReadAllBytes(filePath);
return File(bytes, mimeType, fileName);
}
}