I'm trying to get simple ASP.NET CORE web app going and I'm running into issues with allowing the user to access a file on my server via a html link. I have a model called "TestModel", a view called "TestView" and a controller called "AppController". My view allows the user to input some text in two separate fields as well as select two different files from their hard drive, it binds everything to my model and performs a POST when the user clicks a button. I have verified that the model is correctly being passed back to my controller. My controller correctly saves the files to a folder in my server directory, uses a separate service to manipulate the files, and returns the model back to the view, i.e. when I use the debugger to inspect "return View(model)" in the Testview action the model being passed back has all it's properties populated.
The issue is that I want two links on the view to point to the files on the server so that the user can click the link and receive a prompt to download the files, but I can't seem to get it going. I am using the @Html.ActionLink() capability in razor to point to a "Download descriptions" action and pass the model to it, thinking that it would be passing the current view's model, but the model it passes has all fields as null. Any insight into what I am doing incorrectly?
These are the relevant actions in my AppController:
[HttpPost("~/App/TestView")]
public IActionResult TestView(TestModel Model)
{
if (ModelState.IsValid)
{
Console.WriteLine("Something was sent back from the TestView page");
Model.OnPostAsync();
var x = _descLogicService.Convert(Model);
}
return View(Model);
}
public IActionResult DownloadDescriptions(TestModel model)
{
var cd = new ContentDispositionHeaderValue("attachment")
{
FileNameStar = model.DescriptionFile.FileName
};
Response.Headers.Add(HeaderNames.ContentDisposition, cd.ToString());
byte[] bytes = System.IO.File.ReadAllBytes(model.MeasurementExportFile);
using (FileStream fs = new FileStream(model.MeasurementExportFile, FileMode.Open, FileAccess.Read))
{
fs.Read(bytes, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
}
return File(bytes, "text/csv");
}
Here is my View Model:
public class TestModel
{
[BindProperty]
[Required]
[MinLength(5)]
public string Name { get; set; }
[BindProperty]
[Required]
[MaxLength(10,ErrorMessage ="Input string is too long.")]
public string MyInput { get; set; }
[BindProperty]
[Required]
public IFormFile DescriptionFile { get; set; }
[BindProperty]
[Required]
public IFormFile MeasurementFile { get; set; }
public string DescriptionDirectory { get; set; }
public string DescriptionExportFile { get; set; }
public string MeasurementDirectory { get; set; }
public string MeasurementExportFile { get; set; }
public async Task OnPostAsync()
{
var token = DateTime.Now.Ticks.ToString();
var directory = Directory.CreateDirectory(@"uploads\"+ token + @"\");
DescriptionDirectory = directory.CreateSubdirectory("Descriptions").FullName + @"\";
DescriptionExportFile = directory.CreateSubdirectory(@"Descriptions\Exports\").FullName + DescriptionFile.FileName;
MeasurementDirectory = directory.CreateSubdirectory("Measurements").FullName + @"\";
MeasurementExportFile = directory.CreateSubdirectory(@"Measurements\Exports\").FullName + MeasurementFile.FileName;
var file = Path.Combine(DescriptionDirectory, DescriptionFile.FileName);
using (var fileStream = new FileStream(file, FileMode.Create))
{
await DescriptionFile.CopyToAsync(fileStream).ConfigureAwait(true);
}
file = Path.Combine(MeasurementDirectory, MeasurementFile.FileName);
using (var fileStream = new FileStream(file, FileMode.Create))
{
await MeasurementFile.CopyToAsync(fileStream).ConfigureAwait(true);
}
}
}
And here is the View:
@**I need to add the namespace of C# models I'm creating *@
@using FirstASPNETCOREProject.ViewModels
@*I need to identify the model which 'fits' this page, that is the properties of the model can be
bound to entities on the view page, using "asp-for"*@
@model TestModel
@{
ViewData["Title"] = "Page for File Uploads";
}
@section Scripts{
}
<div asp-validation-summary="ModelOnly" style="color:white"></div>
<form method="post" enctype="multipart/form-data">
<label>Enter a Description File Name</label>
<input asp-for="Name" type="text" />
<span asp-validation-for="Name"></span>
<br>
<label>Select a Description File</label>
<input asp-for="DescriptionFile" type="file" />
<span asp-validation-for="DescriptionFile"></span>
<br>
<label>Enter the Measurement File Name</label>
<input asp-for="MyInput" type="text">
<span asp-validation-for="MyInput"></span>
<br>
<label>Select a Measurement File</label>
<input asp-for="MeasurementFile" type="file">
<span asp-validation-for="MeasurementFile"></span>
<br>
<input type="submit" value="Send Message" />
</form>
@Html.ActionLink("Description File", "DownloadDescriptions", "App")
@Html.ActionLink("Measurement File", "DownloadMeasurements", "App")
