51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using DXApp.TemplateKitProject.Models;
|
|
using DXApp.TemplateKitProject.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
namespace DXApp.TemplateKitProject.Pages.Invoices;
|
|
|
|
public class UploadModel(
|
|
PdfAttachmentExtractorService extractor,
|
|
ILogger<UploadModel> logger) : PageModel
|
|
{
|
|
[BindProperty]
|
|
public IFormFile? PdfFile { get; set; }
|
|
|
|
public PdfExtractionResult? Result { get; private set; }
|
|
public bool ExtractionDone { get; private set; }
|
|
public string? ErrorMessage { get; private set; }
|
|
|
|
public void OnGet()
|
|
{ }
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (PdfFile is null || PdfFile.Length == 0)
|
|
{
|
|
ModelState.AddModelError(nameof(PdfFile), "Bitte eine PDF-Datei auswählen.");
|
|
return Page();
|
|
}
|
|
|
|
if (!PdfFile.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
ModelState.AddModelError(nameof(PdfFile), "Nur PDF-Dateien sind erlaubt.");
|
|
return Page();
|
|
}
|
|
|
|
ExtractionDone = true;
|
|
|
|
try
|
|
{
|
|
await using var stream = PdfFile.OpenReadStream();
|
|
Result = extractor.ExtractAttachments(stream, PdfFile.FileName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger.LogError(ex, "Fehler beim Verarbeiten der Datei '{FileName}'.", PdfFile.FileName);
|
|
ErrorMessage = $"Fehler beim Verarbeiten der Datei: {ex.Message}";
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
} |