[Table("tbUserPhoto")]
public partial class tbUserPhoto
{
[Key]
[StringLength(50)]
public string UserName { get; set; }
public byte[] UserPhoto { get; set; }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = "UserPhoto")] tbUserPhoto tbUserPhoto)
{
byte[] imageData = null;
if (Request.Files.Count > 0)
{
HttpPostedFileBase ImgFile = Request.Files["UserPhoto"];
using (var binary = new BinaryReader(ImgFile.InputStream))
{
imageData = binary.ReadBytes(ImgFile.ContentLength);
}
}
if (ModelState.IsValid)
{
tbUserPhoto.UserPhoto = imageData;
db.tbUserPhoto.Add(tbUserPhoto);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tbUserPhoto);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserName")] tbUserPhoto tbUserPhoto)
{
////...........Burası HttpPostedFileBase UserFoto parametresi olmadan...................................................
byte[] imageData = null;
if (Request.Files.Count > 0)
{
HttpPostedFileBase ImgFile = Request.Files[0];
using (var binary = new BinaryReader(ImgFile.InputStream))
{
imageData = binary.ReadBytes(ImgFile.ContentLength);
}
}
if (ModelState.IsValid)
{
db.Entry(tbUserPhoto).State = EntityState.Modified;
if (imageData != null)
{
tbUserPhoto.UserPhoto = imageData;
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(tbUserPhoto);
}
//........ EDIT FORM html
@using (Html.BeginForm("Edit", "UserPhotos", null, FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>tbUserPhoto</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.UserName)
<div class="form-group">
@Html.LabelFor(model => model.UserPhoto, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (Model.UserPhoto != null)
{
<img src="data:image/png;base64,
@Convert.ToBase64String(Model.UserPhoto,
Base64FormattingOptions.None)"
class="img-circle" width="100" height="100"
alt="Image" />
}
else
{
<img src="~/images/no_user.png"
class="img-circle" width="100" height="100"
alt="Image" />
}
<p> Değiştir:</p>
<input type="file" id="fileFotografi" name="Fotografi" accept=".png,.jpg,.jpeg,.gif" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
//******************** .NET CORE ****************
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(string id, [Bind("UserName")] TbUserPhoto tbUserPhoto, IFormFile Fotografi)
{
if (id != tbUserPhoto.UserName)
{
return NotFound();
}
//.......................................................
byte[] imageData = null;
if (Fotografi != null)
{
using (var binary = new BinaryReader(Fotografi.OpenReadStream()))
{
imageData = binary.ReadBytes((Int32)Fotografi.Length);
}
}
//.......................................................
if (ModelState.IsValid)
{
try
{
//.......................................................
tbUserPhoto.UserPhoto = imageData;
//.......................................................
_context.Update(tbUserPhoto);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!TbUserPhotoExists(tbUserPhoto.UserName))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(tbUserPhoto);
}
//*********** Edit Form chtml ***********
<form asp-action="Edit" enctype="multipart/form-data" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="UserName" />
<div class="form-group">
<label asp-for="UserPhoto" class="control-label"></label>
@if (Model.UserPhoto != null)
{
<img src="data:image/png;base64,
@Convert.ToBase64String(Model.UserPhoto,
Base64FormattingOptions.None)"
class="img-circle" width="100" height="100"
alt="Image" />
}
else
{
<img src="~/images/no_user.png"
class="img-circle" width="100" height="100"
alt="Image" />
}
<span asp-validation-for="UserPhoto" class="text-danger"></span>
<hr />
<p> Değiştir:</p>
<input type="file" id="fileFotografi" name="Fotografi" accept=".png,.jpg,.jpeg,.gif" />
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
Yorumlar
Yorum Gönder