Files
Mapo-IOB/SMGen/Pages/Download.cshtml.cs
2023-07-25 11:54:11 +02:00

53 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace SMGen.Pages
{
/// <summary>
/// Gestione donwnload file csv
///
/// da valutare eventualmente xlsx con https://github.com/closedxml/closedxml
/// </summary>
public class DownloadModel : PageModel
{
#region Private Fields
private readonly IWebHostEnvironment _env;
#endregion Private Fields
#region Public Constructors
public DownloadModel(IWebHostEnvironment env)
{
_env = env;
}
#endregion Public Constructors
#region Public Methods
public IActionResult OnGet()
{
string fileName = "";
if (Request.Query.ContainsKey("fileName"))
{
fileName = Request.Query["fileName"];
}
var filePath = Path.Combine(_env.ContentRootPath, fileName);
//var filePath = Path.Combine(_env.WebRootPath, "..\\temp\\", fileName);
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
return File(fileBytes, "application/force-download", fileName.Split("\\").Last());
}
#endregion Public Methods
}
}