Asp net MVC Lưu file (Savefile)
@using(Html.BeginForm("ThemMoi", "KhachHang", FormMethod.Post, new { enctype = "multipart/form-data" } ))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<label class="control-label col-md-2">Hình ảnh</label>
<div class="col-md-10">
<input type="file" name="FileAnh" />
@Html.ValidationMessageFor(model => model.UrlHinhAnh, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-success btn-sm">Lưu</button>
</div>
</div>
</div>
}
[HttpPost]
public ActionResult ThemMoi(HttpPostedFileBase fileAnh)
{
if(fileAnh.ContentLength > 0)
{
// Lưu file
string rootFolder = Server.MapPath("/Data/");
string pathImage = rootFolder + fileAnh.FileName;
fileAnh.SaveAs(pathImage);
}
return RedirectToAction("DanhSach");
}