add for allow header for dowload file on client Content-Disposition

This commit is contained in:
Kham
2021-08-23 17:08:27 +10:00
parent d90e73dfcc
commit 4792acea76
6 changed files with 123 additions and 7 deletions
@@ -6,6 +6,7 @@ using CarParkValidationAPI.Interface;
using System.Threading.Tasks; using System.Threading.Tasks;
using System; using System;
using Microsoft.AspNetCore.StaticFiles; using Microsoft.AspNetCore.StaticFiles;
using CarParkValidationAPI.Repository;
namespace CarParkValidationAPI.Controllers namespace CarParkValidationAPI.Controllers
{ {
@@ -14,10 +15,15 @@ namespace CarParkValidationAPI.Controllers
[Route("api/v1/[controller]")] [Route("api/v1/[controller]")]
public class ConcessionValidationController : ControllerBase public class ConcessionValidationController : ControllerBase
{ {
private ConcessionValidationReportRepository _concessionValidationReport;
private IConcessionValidation _concessionValidation; private IConcessionValidation _concessionValidation;
private readonly FileExtensionContentTypeProvider _contentTypeProvider; private readonly FileExtensionContentTypeProvider _contentTypeProvider;
public ConcessionValidationController(IConcessionValidation concessionValidation)
public ConcessionValidationController(IConcessionValidation concessionValidation,
ConcessionValidationReportRepository concesionReportReport)
{ {
_concessionValidationReport = concesionReportReport;
_concessionValidation = concessionValidation; _concessionValidation = concessionValidation;
} }
@@ -33,6 +39,7 @@ namespace CarParkValidationAPI.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> ConcessionValidation(bool onlyActive=true) public async Task<IActionResult> ConcessionValidation(bool onlyActive=true)
{ {
var retval = await _concessionValidation.LoadConcessionValidationAsync(onlyActive); var retval = await _concessionValidation.LoadConcessionValidationAsync(onlyActive);
return Ok(retval); return Ok(retval);
} }
@@ -73,8 +80,9 @@ namespace CarParkValidationAPI.Controllers
public async Task<IActionResult> GetReportFile(DateTime fromDate , DateTime toDate) public async Task<IActionResult> GetReportFile(DateTime fromDate , DateTime toDate)
{ {
var result = await _concessionValidation.GetReport(fromDate, toDate); // this.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
string fileContentType = Map(result.Data.FileName); var result = await _concessionValidationReport.GetConcessionValidationReport(fromDate, toDate); // _concessionValidation.GetReport(fromDate, toDate);
string fileContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //Map(result.Data.FileName);
return File(result.Data.Content, fileContentType, result.Data.FileName); return File(result.Data.Content, fileContentType, result.Data.FileName);
} }
@@ -16,7 +16,7 @@
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "api/v1/LookupCode/ConcessionTypes", "launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
@@ -0,0 +1,103 @@
using CarParkValidationAPI.Datas;
using CarParkValidationAPI.Models;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SpreadsheetLight;
using CarParkValidationAPI.Entities;
using System.IO;
namespace CarParkValidationAPI.Repository
{
public class ConcessionValidationReportRepository
{
private readonly DataContext _context;
private readonly IOptions<AppSettings> _appSettings;
const string DateFormat = "dd-MM-yyyy";
public ConcessionValidationReportRepository(
DataContext context, IOptions<AppSettings> appSettings)
{
_context = context;
_appSettings = appSettings;
}
public async Task<ResultModel<FileContent>> GetConcessionValidationReport(DateTime fromDate, DateTime toDate)
{
//excel SLDocument sl = new SLDocument();
int statusCode = 1;
ConcessionValidationExtDto item;
int col = 1;
int row = 1;
SLDocument sl = new SLDocument();
// add the title first
sl.SetCellValue(row, col++, "Date");
sl.SetCellValue(row, col++, "Concession Card");
sl.SetCellValue(row, col++, "Concession Holder");
sl.SetCellValue(row, col++, "ConcessionExpiry");
sl.SetCellValue(row, col++, "ConcessionType");
sl.SetCellValue(row, col++, "ValidationPiont");
sl.SetCellValue(row, col++, "StaffLink No");
//format date
SLStyle style = sl.CreateStyle();
style.FormatCode = "dd/mm/yyyy hh:mm:ss";
sl.SetColumnStyle(1, style);
style = sl.CreateStyle();
style.Font.Bold = true;
sl.SetRowStyle(1, style);
var list = await _context.SelectConcessionValidationByDate(true, fromDate.ToLocalTime(), toDate.ToLocalTime());
for (int i = 0; i < list.Count; i++)
{
col = 1;
item = list[i];
row = i + 2;
sl.SetCellValue(row, col++, item.ValidateDate);
sl.SetCellValue(row, col++, item.ConcessionCard);
sl.SetCellValue(row, col++, item.ConcessionHolder);
sl.SetCellValue(row, col++, item.ConcessionExpiry);
sl.SetCellValue(row, col++, item.ConcessionType);
sl.SetCellValue(row, col++, item.ValidationPiont);
sl.SetCellValue(row, col++, item.StafflinkNo);
}
byte[] array;
// sl.SaveAs("C:\\Temp\\Report\\ConcessionValidationDate" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx");
using (MemoryStream ws = new MemoryStream())
{
sl.SaveAs(ws);
array = ws.ToArray();
/*
using (FileStream fs = new FileStream("c:\\temp\\Report\\conReport.xlsx",FileMode.Create,FileAccess.Write))
{
ws.WriteTo(fs);
fs.Close();
}
*/
ws.Close();
}
var result = new FileContent
{
Content = array,
FileName = "Report" +DateTime.Now.ToString("yyyy_MM_dd_HH_mm")+ ".xlsx",
};
return new ResultModel<FileContent>
{
Data = result,
StatusCode = statusCode
};
}
}
}
@@ -23,7 +23,6 @@ namespace CarParkValidationAPI.Repository
public ConcessionValidationRepository( public ConcessionValidationRepository(
DataContext context, DataContext context,
IOptions<AppSettings> appSettings) IOptions<AppSettings> appSettings)
{ {
_context = context; _context = context;
@@ -70,7 +69,7 @@ namespace CarParkValidationAPI.Repository
model.ConcessionTypeID = dto.ConcessionTypeId; model.ConcessionTypeID = dto.ConcessionTypeId;
model.ValidationID = dto.Id; model.ValidationID = dto.Id;
model.StafflinkNo = dto.StafflinkNo; model.StafflinkNo = dto.StafflinkNo;
model.ValidateDate = dto.ValidateDate; model.ValidateDate = dto.ValidateDate.ToLocalTime();
model.ValidationPointID = dto.ValidationPointId; model.ValidationPointID = dto.ValidationPointId;
return model; return model;
} }
@@ -255,6 +254,7 @@ namespace CarParkValidationAPI.Repository
{ {
int statusCode = 1; int statusCode = 1;
string fileContext = "filecontain as string this is file contain in string format"; string fileContext = "filecontain as string this is file contain in string format";
//await this._concessionValidationReport.GetConcessionValidationReport(DateTime.Today.AddDays(-30), DateTime.Today.AddDays(30));
var result = new FileContent var result = new FileContent
{ {
Content = Convert.FromBase64String(fileContext), Content = Convert.FromBase64String(fileContext),
@@ -64,7 +64,9 @@ namespace CarParkValidationAPI.Repository
myUser.LastName = loginInfo.LastName; myUser.LastName = loginInfo.LastName;
myUser.Username = loginInfo.StafflinkNo; myUser.Username = loginInfo.StafflinkNo;
//now check the adminuser table //now check the adminuser table
var user = _context.AdminUsers.SingleOrDefault(x => x.StafflinkNo == model.Username); var user = _context.AdminUsers.
SingleOrDefault(x => x.StafflinkNo == model.Username
&& x.Active == true);
if (user != null) if (user != null)
{ {
myUser.Role = user.RoleType; myUser.Role = user.RoleType;
+3
View File
@@ -90,6 +90,7 @@ namespace CarParkValidationAPI
services.AddScoped<IAdminUser, AdminUserRepository>(); services.AddScoped<IAdminUser, AdminUserRepository>();
services.AddScoped<IConcessionValidation, ConcessionValidationRepository>(); services.AddScoped<IConcessionValidation, ConcessionValidationRepository>();
services.AddScoped<ILookupCode, LookupCodeRepository>(); services.AddScoped<ILookupCode, LookupCodeRepository>();
services.AddScoped(typeof(ConcessionValidationReportRepository));
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -117,6 +118,8 @@ namespace CarParkValidationAPI
.SetIsOriginAllowed(origin => true) .SetIsOriginAllowed(origin => true)
.AllowAnyMethod() .AllowAnyMethod()
.AllowAnyHeader() .AllowAnyHeader()
.WithExposedHeaders("Content-Disposition")
.AllowCredentials()); .AllowCredentials());
// global error handler // global error handler