Hello, when posting this answer I'm assuming you're using ASP.NET MVC Core.
I've created an extremely simple solution just so you can absorb some basics.
First we will create the Models Report Municipality and Municipality :
public class RelatorioMunicipio
{
public RelatorioMunicipio(string caminhoBaseArquivo)
{
CaminhoBase = caminhoBaseArquivo;
}
public string CaminhoBase { get; set; }
public Municipio Municipio { get; set; }
public string AbrirArquivo()
{
return System.IO.File.ReadAllText(System.IO.Path.Combine(CaminhoBase, Municipio.Nome + ".txt"));
}
}
using Microsoft.AspNetCore.Http;
public class Municipio
{
public string Nome { get; set; }
public IFormFile Arquivo{ get; set; }
}
This will first change your view where you upload your file:
@model UploadArquivos.Model.Municipio
<form asp-action="GerarRelatorio" asp-controller="Home" enctype="multipart/form-data">
<div class="form-group">
<label for="municipio">Nome do Município:</label>
<input type="text" class="form-control" asp-for="Nome" as placeholder="Nome do Município">
</div>
<div class="form-group">
<label for="arquivo">Selecionar arquivo...</label>
<input type="file" asp-for="Arquivo">
<p class="help-block">Selecione no seu computador o arquivo BPA</p>
</div>
<button type="submit" class="btn btn-default">Gerar relatório</button>
</form>
In the example I created this view using the same Home and Index.
Note that you set your form as enctype="multipart / form-data" , this is to inform ASP.NET that your form will work with uploading files.
The @model .... Municipio statement indicates that we are working with an object of this type passed by the action of your View, this enables us to use the asp-for="PropertyName" , making your fields automatically map to the properties in your post.
Now let's go to the controllers:
using Microsoft.AspNetCore.Mvc;
using UploadArchives.Model;
using Microsoft.AspNetCore.Hosting;
using System.IO;
public class HomeController: Controller
{
IHostingEnvironment Enviroment {get; set; }
public HomeController(IHostingEnvironment enviroment)
{
Enviroment = enviroment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult GerarRelatorio(Municipio municipio)
{
if (municipio.Arquivo != null)
{
// salvando arquivo em disco
var reader = new BinaryReader(municipio.Arquivo.OpenReadStream());
System.IO.File.WriteAllBytes(System.IO.Path.Combine(Enviroment.WebRootPath, $"{municipio.Nome}.txt"), reader.ReadBytes((int)municipio.Arquivo.Length));
}
return RedirectToAction("Exibir", "Relatorio", new { nome = municipio.Nome });
}
}
At Home we need the IHostingEnvironment that is in the Microsoft.AspNetCore.Hosting namespace , we use it only to get the root path of the application to save your file
In the GerarReport method, notice that we have a Municipio parameter, this is automatically assigned by the Framework when we perform the post due to what I said above.
Note that when we created your Municipio model in addition to the name we created an IFormFile property called File, it is in this property that ASP.NET will assign the posted file, and with it in hand we saved to disk with the method WriteAllBytes.
Done this we redirected to your Action Display in Controller Reporting informing the name of the municipality as a parameter.
And in the Reporting controller we have:
public class RelatorioController : Controller
{
IHostingEnvironment Enviroment { get; set; }
public RelatorioController(IHostingEnvironment enviroment)
{
Enviroment = enviroment;
}
[Route("Relatorio/Exibir/{nome}")]
public IActionResult Exibir(string nome)
{
RelatorioMunicipio relatorio = new RelatorioMunicipio(Enviroment.WebRootPath);
relatorio.Municipio = new Municipio() { Nome = nome };
return View(relatorio);
}
}
Here we only start an object of type ReportMunicipal filling its properties. But just like in Home, I need to tell WebRootPath to tell the object where it should look for the file.
Finally we have the View of the report
@model UploadArquivos.Model.RelatorioMunicipio
<h1>@Model.Municipio.Nome</h1>
@Model.AbrirArquivo()
In it we simply inform the type of data that will be treated, we display the name of the town and we get the generated file.
For a better understanding, I published this project on GitHub, download and any questions post in the comments.
I hope I have helped.