I am trying to save image file to a folder under wwwroot, the database should store the path name of the file, and the folder under wwwroot should store the image file.
What I want to achieve is to save the image file to the folder under wwwroot. and the path name to the image file to be save to the local database.
Thank you!!
View:
<form asp-controller="MobileSuits" asp-action="Add">
<div class="form-horizontal">
<h4>TheMobileSuit</h4>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Id" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Id" class="form-control" />
<span asp-validation-for="Id" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="Name" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="PicFile" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*<input type="file" name="photo"
class="form-control" />*@
<input type="file" name="file" id="file" />
<span asp-validation-for="PicFile" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
Controller:
[Authorize]
public IActionResult Add()
{
return View();
}
[Authorize]
[HttpPost]
public IActionResult Add(TheMobileSuit themobilesuits, IFormFile photo)
{
DbSet<TheMobileSuit> dbs = _dbContext.TheMobileSuit;
dbs.Add(themobilesuits);
if (_dbContext.SaveChanges() == 1)
TempData["Msg"] = "New Order Added!";
else
TempData["Msg"] = "Error.";
return RedirectToAction("Index");
}