PDF könyvtár letöltése

This commit is contained in:
2025-04-16 13:29:07 +02:00
parent a1d683b955
commit 5acf9ff954
10 changed files with 279 additions and 4 deletions
@@ -93,6 +93,58 @@ namespace WorkFlowCheck.API.Controllers
} }
return retVal; return retVal;
} }
[HttpGet("GetAllPDFFiles")]
public async Task<ApiResponseDTO<List<ImageFileDTO>>> GetAllPDFilesAsync()
{
var retVal = new ApiResponseDTO<List<ImageFileDTO>>()
{
IsSuccess = true,
};
try
{
var imageFolder = Path.Combine(Directory.GetCurrentDirectory(), "PDF");
var files = Directory.GetFiles(imageFolder)
.Select(f =>
{
var fileInfo = new FileInfo(f);
return new
{
FileName = fileInfo.Name,
FileSize = FormatFileSize(fileInfo.Length),
CreatedDate = fileInfo.CreationTime.ToString("yyyy.MM.dd HH:mm"),
};
});
var checkListFileInfos = new List<ImageFileDTO>();
foreach (var file in files.OrderBy(o => o.FileName).ToList())
{
var checkListFileInfoDTO = new ImageFileDTO()
{
FileName = file.FileName,
FileSize = file.FileSize,
CreateDate = file.CreatedDate,
};
checkListFileInfos.Add(checkListFileInfoDTO);
}
if (checkListFileInfos != null)
{
retVal.IsSuccess = true;
retVal.Data = checkListFileInfos;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("DownloadFile/{filename}")] [HttpGet("DownloadFile/{filename}")]
public async Task<ApiResponseDTO<byte[]>> DownloadFile(string filename) public async Task<ApiResponseDTO<byte[]>> DownloadFile(string filename)
@@ -112,6 +164,38 @@ namespace WorkFlowCheck.API.Controllers
byte[] response = System.IO.File.ReadAllBytes(imageFile); byte[] response = System.IO.File.ReadAllBytes(imageFile);
if (response != null)
{
retVal.IsSuccess = true;
retVal.Data = response;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("DownloadPDFFile/{filename}")]
public async Task<ApiResponseDTO<byte[]>> DownloadPDFFile(string filename)
{
var retVal = new ApiResponseDTO<byte[]>()
{
IsSuccess = false,
};
var imageFile = Path.Combine(Directory.GetCurrentDirectory(), "PDF", filename);
if (!System.IO.File.Exists(imageFile))
{
retVal.Errors.Add($"{filename} does not exist");
return retVal;
}
byte[] response = System.IO.File.ReadAllBytes(imageFile);
if (response != null) if (response != null)
{ {
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WorkFlowCheck.Common.DTO
{
public class PDFFileDTO
{
public string Id { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public string CreateDate { get; set; }
}
}
@@ -3,7 +3,7 @@
@using WorkFlowCheck.Common.Helper @using WorkFlowCheck.Common.Helper
@using WorkFlowCheck.Common.DTO @using WorkFlowCheck.Common.DTO
@{ @{
ViewData["Title"] = "Berendezések"; ViewData["Title"] = "Képek";
} }
<h1>@ViewData["Title"]</h1> <h1>@ViewData["Title"]</h1>
@@ -0,0 +1,80 @@
@page
@model WorkFlowCheck.Web.Pages.BaseStock.PDFFiles.PDFFilesPageModel
@using WorkFlowCheck.Common.Helper
@using WorkFlowCheck.Common.DTO
@{
ViewData["Title"] = "PDF fájlok";
}
<h1>@ViewData["Title"]</h1>
<div class="card shadow p-4">
<table id="tbPDFFilesPage" class="table table-bordered table-hover table-sm" style="width:100%">
<thead class="table-primary">
<tr>
<th>ID</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.FileName), typeof(PDFFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.CreateDate), typeof(PDFFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.FileSize), typeof(PDFFileDTO))</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tfoot class="table-light">
<tr>
<th>ID</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.FileName), typeof(PDFFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.FileSize), typeof(PDFFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(PDFFileDTO.CreateDate), typeof(PDFFileDTO))</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
@section Scripts {
<script>
const table = new DataTable('#tbPDFFilesPage', {
ajax: {
url: "@Url.Page("./PDFFilesPage", "LoadPDFFiles")",
type: "GET",
dataSrc : "data"
},
columns: [
{ data: "id" },
{ data: "fileName" },
{ data: "createDate" },
{ data: "fileSize" },
{ data: null, render: function (data, type, row) {
return renderActionButtonsDownloadPDFs(row.id);
}}
],
columnDefs: [
{
"targets": 0,
"visible": false
},
{
"targets": 2,
"className": "text-start"
},
{
"targets": 3,
"className": "text-end"
},
{
"targets": 4,
"className": "text-center",
"width": "10%"
}
],
processing:true,
language: { url: '//cdn.datatables.net/plug-ins/2.2.2/i18n/hu.json',}
});
$('#tbPDFFilesPage').on('click', '.download-btn', function ()
{
const row = table.row($(this).closest('tr')).data();
window.location.href = `@Url.Page("/BaseStock/PDFFiles/PDFFilesPage")?handler=DownloadFile&filename=${row.fileName}`;
});
</script>
}
@@ -0,0 +1,47 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Pages.BaseStock.PDFFiles
{
[Authorize]
public class PDFFilesPageModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly ISystemService _systemService;
public PDFFilesPageModel(ILogger<IndexModel> logger, ISystemService systemService)
{
_logger = logger;
_systemService = systemService;
}
public async Task OnGet()
{
}
public async Task<JsonResult> OnGetLoadPDFFiles()
{
var results = await _systemService.GetAllPDFFilesAsync();
return new JsonResult(new { data = results });
}
public async Task<IActionResult> OnGetDownloadFile(string filename)
{
var response = await _systemService.DownloadPDFFile(filename);
if (response.IsSuccess)
{
var result = new FileContentResult(response.Data, "application/pdf")
{
FileDownloadName = filename,
FileContents = response.Data
};
return result;
}
else
{
return new JsonResult(new { success = false });
}
}
}
}
@@ -38,6 +38,7 @@
<li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/Locations/LocationsPage">Lokációk</a></li> <li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/Locations/LocationsPage">Lokációk</a></li>
<li><hr class="dropdown-divider"></li> <!-- EZ AZ ELVÁLASZTÓ VONAL --> <li><hr class="dropdown-divider"></li> <!-- EZ AZ ELVÁLASZTÓ VONAL -->
<li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/ImageFiles/ImageFilesPage">Fényképek</a></li> <li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/ImageFiles/ImageFilesPage">Fényképek</a></li>
<li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/PDFFiles/PDFFilesPage">PDF állomány</a></li>
</ul> </ul>
</li> </li>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
@@ -6,5 +6,7 @@ namespace WorkFlowCheck.Web.Services.Interfaces
{ {
Task<List<ImageFileDTO>> GetAllImageFilesAsync(); Task<List<ImageFileDTO>> GetAllImageFilesAsync();
Task<ApiResponseDTO<byte[]>> DownloadFile(string filename); Task<ApiResponseDTO<byte[]>> DownloadFile(string filename);
Task<List<PDFFileDTO>> GetAllPDFFilesAsync();
Task<ApiResponseDTO<byte[]>> DownloadPDFFile(string filename);
} }
} }
@@ -51,5 +51,47 @@ namespace WorkFlowCheck.Web.Services
} }
return retVal; return retVal;
} }
public async Task<List<PDFFileDTO>> GetAllPDFFilesAsync()
{
var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllPDFFiles";
var retVal = new List<PDFFileDTO>();
try
{
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<PDFFileDTO>>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
return response.Data;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
public async Task<ApiResponseDTO<byte[]>> DownloadPDFFile(string filename)
{
var retVal = new ApiResponseDTO<byte[]>();
var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadPDFFile/{filename}";
try
{
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<byte[]>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
return response;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
} }
} }
@@ -45,7 +45,4 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory> <CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Pages\BaseStock\PDFFiles\" />
</ItemGroup>
</Project> </Project>
+6
View File
@@ -174,6 +174,12 @@ function renderActionButtonsDownloadImages(rowId) {
<i class="bi bi-file-earmark-arrow-down"></i> <i class="bi bi-file-earmark-arrow-down"></i>
</button>`; </button>`;
} }
function renderActionButtonsDownloadPDFs(rowId) {
return `
<button class="btn btn-primary download-btn btn-sm" data-id="${rowId}">
<i class="bi bi-file-earmark-arrow-down"></i>
</button>`;
}
function renderActionButtonsforCheckListHeader(data, rowId) { function renderActionButtonsforCheckListHeader(data, rowId) {
if (data === 4) { if (data === 4) {
return ` return `