When I click on the input and select a file, if the file size is small the viewModel returns non-null and saves the file in myfiles folder.
But if the file size is large, the controller is called before the file upload is not complete and the viewmodel returns null.
cshtml
<form class="form" method="post" enctype="multipart/form-data">
<div class="form-group">
<input asp-for="FirstName" class="form-control"/>
<span asp-validation-for="FirstName" class="form-text text-danger"></span>
</div>
<div class="form-group">
<input asp-for="LastName" class="form-control"/>
<span asp-validation-for="LastName" class="form-text text-danger"></span>
</div>
<input type="file" asp-for="MyFile"/>
</div>
<button type="submit" class="btn btn-primary mr-2">Save</button>
</form>
viewModel
public class MyCarViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IFormFile MyFile{ set; get; }
}
controller
[HttpPost]
public async Task<IActionResult> AddCar (MyCarViewModel viewModel)
var fileName = "carfiles" + Path.GetExtension(viewModel.MyFile.FileName);
var path = Path.Combine(Directory.GetCurrentDirectory(), "myfiles", fileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await viewModel.MyFile.CopyToAsync(stream);
}
var addViewModel = new MyCarViewModel()
{
FirstName = viewModel.FirstName,
LastName = viewModel.LastName,
UploadDocumentName = fileName,
};
dbContext.Car.Add(addViewModel);
dbContext.SaveChanges();