Fényképek letöltése programból

This commit is contained in:
2025-04-16 12:34:00 +02:00
parent 335f00367d
commit a1d683b955
10 changed files with 326 additions and 6 deletions
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using WorkFlowCheck.BL.Services.Interfaces;
using WorkFlowCheck.Common.DTO;
@@ -39,5 +40,104 @@ namespace WorkFlowCheck.API.Controllers
return retVal;
}
[HttpGet("GetAllImageFiles")]
public async Task<ApiResponseDTO<List<ImageFileDTO>>> GetAllImageFilesAsync()
{
var retVal = new ApiResponseDTO<List<ImageFileDTO>>()
{
IsSuccess = true,
};
try
{
var imageFolder = Path.Combine(Directory.GetCurrentDirectory(), "Images");
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}")]
public async Task<ApiResponseDTO<byte[]>> DownloadFile(string filename)
{
var retVal = new ApiResponseDTO<byte[]>()
{
IsSuccess = false,
};
var imageFile = Path.Combine(Directory.GetCurrentDirectory(), "Images", 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)
{
retVal.IsSuccess = true;
retVal.Data = response;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
private string FormatFileSize(long bytes)
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = bytes;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order++;
len /= 1024;
}
return $"{len:0.##} {sizes[order]}";
}
}
}
@@ -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 ImageFileDTO
{
public string Id { get; set; }
public string FileName { get; set; }
public string FileSize { get; set; }
public string CreateDate { get; set; }
}
}
@@ -0,0 +1,80 @@
@page
@model WorkFlowCheck.Web.Pages.BaseStock.ImageFiles.ImageFilesPageModel
@using WorkFlowCheck.Common.Helper
@using WorkFlowCheck.Common.DTO
@{
ViewData["Title"] = "Berendezések";
}
<h1>@ViewData["Title"]</h1>
<div class="card shadow p-4">
<table id="tbImageFilesPage" class="table table-bordered table-hover table-sm" style="width:100%">
<thead class="table-primary">
<tr>
<th>ID</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.FileName), typeof(ImageFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.CreateDate), typeof(ImageFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.FileSize), typeof(ImageFileDTO))</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tfoot class="table-light">
<tr>
<th>ID</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.FileName), typeof(ImageFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.FileSize), typeof(ImageFileDTO))</th>
<th>@DisplayNameHelper.GetDisplayName(nameof(ImageFileDTO.CreateDate), typeof(ImageFileDTO))</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
@section Scripts {
<script>
const table = new DataTable('#tbImageFilesPage', {
ajax: {
url: "@Url.Page("./ImageFilesPage", "LoadImageFiles")",
type: "GET",
dataSrc : "data"
},
columns: [
{ data: "id" },
{ data: "fileName" },
{ data: "createDate" },
{ data: "fileSize" },
{ data: null, render: function (data, type, row) {
return renderActionButtonsDownloadImages(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',}
});
$('#tbImageFilesPage').on('click', '.download-btn', function ()
{
const row = table.row($(this).closest('tr')).data();
window.location.href = `@Url.Page("/BaseStock/ImageFiles/ImageFilesPage")?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.ImageFiles
{
[Authorize]
public class ImageFilesPageModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly ISystemService _systemService;
public ImageFilesPageModel(ILogger<IndexModel> logger, ISystemService systemService)
{
_logger = logger;
_systemService = systemService;
}
public async Task OnGet()
{
}
public async Task<JsonResult> OnGetLoadImageFiles()
{
var results = await _systemService.GetAllImageFilesAsync();
return new JsonResult(new { data = results });
}
public async Task<IActionResult> OnGetDownloadFile(string filename)
{
var response = await _systemService.DownloadFile(filename);
if (response.IsSuccess)
{
var result = new FileContentResult(response.Data, "application/octet-stream")
{
FileDownloadName = filename,
FileContents = response.Data
};
return result;
}
else
{
return new JsonResult(new { success = false });
}
}
}
}
@@ -36,6 +36,8 @@
<li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/CheckPoints/CheckPointsPage">Ellenőrzési pontok</a></li>
<li><a class="dropdown-item" asp-area="" asp-page="/BaseStock/Equipments/EquipmentsPage">Berendezések</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><a class="dropdown-item" asp-area="" asp-page="/BaseStock/ImageFiles/ImageFilesPage">Fényképek</a></li>
</ul>
</li>
<li class="nav-item dropdown">
+1
View File
@@ -24,6 +24,7 @@ builder.Services.AddHttpClient();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IBaseStockService, BaseStockService>();
builder.Services.AddScoped<ICheckListService, CheckListService>();
builder.Services.AddScoped<ISystemService, SystemService>();
builder.Host.UseSerilog();
// Add services to the container.
@@ -0,0 +1,10 @@
using WorkFlowCheck.Common.DTO;
namespace WorkFlowCheck.Web.Services.Interfaces
{
public interface ISystemService
{
Task<List<ImageFileDTO>> GetAllImageFilesAsync();
Task<ApiResponseDTO<byte[]>> DownloadFile(string filename);
}
}
@@ -0,0 +1,55 @@
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Services
{
public class SystemService:BaseService, ISystemService
{
public SystemService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
{
}
public async Task<List<ImageFileDTO>> GetAllImageFilesAsync()
{
var endpoint = $"{_httpClient.BaseAddress}api/System/GetAllImageFiles";
var retVal = new List<ImageFileDTO>();
try
{
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<ImageFileDTO>>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
return response.Data;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
public async Task<ApiResponseDTO<byte[]>> DownloadFile(string filename)
{
var retVal = new ApiResponseDTO<byte[]>();
var endpoint = $"{_httpClient.BaseAddress}api/System/DownloadFile/{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,4 +45,7 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Folder Include="Pages\BaseStock\PDFFiles\" />
</ItemGroup>
</Project>
+6
View File
@@ -168,6 +168,12 @@ function renderActionButtons(rowId) {
</button>
`;
}
function renderActionButtonsDownloadImages(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) {
if (data === 4) {
return `