当我在asp mvc 5中上传pic保存在数据库中时,将其保存为此名称
System.Web.HttpPostedFileWrapper
问题是什么?我怎么能解决这个问题?
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>
/ ********************************************** ************** /
答案 0 :(得分:2)
首次设置文件名时,您正在设置model
。但是,当您稍后创建用户时,您正在使用user
。您不能使用model.UserPhoto
执行任何操作,只能将其作为视图返回。