Kianoush Kianoush - 1 month ago 26
ASP.NET (C#) Question

Name photos when uploading photos in the database " System.Web.HttpPostedFileWrapper "

when i upload pic in asp mvc 5 save in database , save it by this name

System.Web.HttpPostedFileWrapper


whats the problem ? how can i solve this ?

public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase UserPhoto)
{
if (ModelState.IsValid)
{
model.DateRegister = DateTime.Now;
var user = new ApplicationUser { UserName = model.UserName, Name = model.Name, Family = model.Family,
PhoneNumber = model.PhoneNumber, Gender = model.Gender, BirthDay = model.BirthDay, DateRegister = model.DateRegister,
IsActive = false, UserPhoto = model.UserPhoto, Email = model.Email };

if (UserPhoto != null)
{
UserPhoto = Request.Files[0];
var ext = System.IO.Path.GetExtension(UserPhoto.FileName);
if (ext == ".jpeg" || ext == ".jpg" || ext == ".png")
{

string filename = model.PhoneNumber + ext;
UserPhoto.SaveAs(Server.MapPath(@"~/Image/" + filename));
model.UserPhoto = filename;
}
}

var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{

await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}


.

<div class="form-group">
@Html.LabelFor(m => m.UserPhoto, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
<input type="file" name="UserPhoto" id="fileUpload" />
</div>
</div>


/*******************************************************************************************/

Answer

When you first set the filename, you are setting model. But when you are later creating the user, you are using user. You don't do anything with model.UserPhoto, other than return it as the view.

Comments