我想用图像创建用户个人资料。该用户可以上传他的照片,在他的个人资料中会有这张照片,在论坛中会有从原始照片创建的小图像。我没有显示图像但调整大小的问题。我在控制器中有此代码,用户可以在其中更改他的信息(姓名,年龄,...)并可以上传照片:
[HttpPost, ValidateInput(false)]
public ActionResult Upravit(int id, FormCollection collection, HttpPostedFileBase file)
{
try
{
var user = repo.Retrieve(id);
if (TryUpdateModel(user))
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Fotky/Profilove/"), (user.Name.Name + " " + user.Name.Surname + Path.GetExtension(fileName)));
file.SaveAs(path);
user.ImagePath = "/Fotky/Profilove/" + user.Name.Name + " " + user.Name.Surname + Path.GetExtension(fileName);
}
repo.Save(user);
return RedirectToAction("Index");
}
return View();
}
catch
{
return View();
}
}
我的观点是这样的:
@using (Html.BeginForm("Upravit", "Uzivatel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>User</legend>
@Html.HiddenFor(model => model.UserID)
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
.
.
<input type="file" name="file" id="file" />
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
正如我所说,我只需要帮助将代码添加到控制器,从原始图像创建小图像并将其保存到。谢谢你的帮助