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
@@ -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(
DataContext context,
IOptions<AppSettings> appSettings)
{
_context = context;
@@ -70,7 +69,7 @@ namespace CarParkValidationAPI.Repository
model.ConcessionTypeID = dto.ConcessionTypeId;
model.ValidationID = dto.Id;
model.StafflinkNo = dto.StafflinkNo;
model.ValidateDate = dto.ValidateDate;
model.ValidateDate = dto.ValidateDate.ToLocalTime();
model.ValidationPointID = dto.ValidationPointId;
return model;
}
@@ -255,6 +254,7 @@ namespace CarParkValidationAPI.Repository
{
int statusCode = 1;
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
{
Content = Convert.FromBase64String(fileContext),
@@ -64,7 +64,9 @@ namespace CarParkValidationAPI.Repository
myUser.LastName = loginInfo.LastName;
myUser.Username = loginInfo.StafflinkNo;
//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)
{
myUser.Role = user.RoleType;