I am Trying To make A form In Ef core Razor pages where I have to upload Salary slip and save it in DB. My Entity Class Is
public class SalarySlip
{
public int Id { get; set; }
//Later It will be a dropdown List Of Employee Type.
public string EmpName { get; set; }
public string SlipName { get; set; }
public byte[] Content { get; set; }
}
my Create Model
public class CreateModel : PageModel
{
private readonly ApplicationDbContext _db;
private readonly IWebHostEnvironment _hostEnvironment;
[BindProperty]
public SalarySlip SalarySlip { get; set; }
public CreateModel(ApplicationDbContext db , IWebHostEnvironment webHostEnvironment)
{
_db = db;
_hostEnvironment = webHostEnvironment;
}
[BindProperty]
public InputModel FileUpload { get; set; }
public class InputModel
{
public IFormFile FormFile { get; set; }
}
public IActionResult OnGet()
{
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
// Uplode file to folder.
if (FileUpload.FormFile.Length > 0)
{
using (var stream = new FileStream(Path.Combine(_hostEnvironment.WebRootPath, "uploadfiles", FileUpload.FormFile.FileName), FileMode.Create))
{
await FileUpload.FormFile.CopyToAsync(stream);
}
}
//save image to dataBase.
using(var memoryStream = new MemoryStream())
{
await FileUpload.FormFile.CopyToAsync(memoryStream);
//uplode the file if Less than 2 MB.
if (memoryStream.Length < 2097152)
{
var file = new SalarySlip()
{
SlipName = FileUpload.FormFile.FileName,
Content = memoryStream.ToArray()
};
_db.SalarySlip.Add(file);
await _db.SaveChangesAsync();
}
else
{
ModelState.AddModelError("SalarySlip", "The File is Too Large.");
}
}
//_db.SalarySlip.Add(SalarySlip);
//await _db.SaveChangesAsync();
return RedirectToPage("Index");
}
}
My cshtml.cs Razor page is
@page
@model New11.Pages.SalarySlips.CreateModel
@{
ViewData["Title"] = "Create";
}
<br />
<h2 class="text-info">Create Biometric </h2>
<br />
<form method="post" enctype="multipart/form-data">
<div class="border backgroundWhite">
<div class="form-group row">
<div class="col-2">
<label asp-for="SalarySlip.EmpName"></label>
</div>
<div class="col-5">
<input asp-for="SalarySlip.EmpName" class="form-control" />
</div>
@*<span asp-validation-for="BiometricAsset.Name" class="text-danger"></span>*@
</div>
<div class="form-group row">
<div class="col-2">
<label asp-for="SalarySlip.SlipName"></label>
</div>
<div class="col-5">
<input asp-for="SalarySlip.SlipName" class="form-control" />
</div>
@*<span asp-validation-for="BiometricAsset.Name" class="text-danger"></span>*@
</div>
<div class="form-group ">
<lebel asp-for="FileUplode.FormFile" class="custom-control-label"></lebel>
<input asp-for="FileUpload.FormFile" class="form-control" type="file" />
<span asp-validation-for="FileUpload.FormFile" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-5 offset-2">
<partial name="_CreateAndBackToListButton" />
</div>
</div>
</form>
@section Scripts{
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
But After Execution I am Getting This Exception
So Plzz suggest What I am Doing Wrong Here?
I already Took Reference From here
https://learn.microsoft.com/en-us/answers/questions/807026/upload-image-to-aspnet-razor-page.html#answer-979687