画像付きのユーザープロファイルを作成したい。そのユーザーは自分の写真をアップロードできます。プロフィールにはこの写真があり、フォーラムには元の写真から作成された小さな画像があります。画像の表示は問題ありませんが、サイズ変更は問題ありません。私はコントローラーにこのコードを持っており、ユーザーは自分の情報(名前、年齢など)を変更したり、写真をアップロードしたりできます。
[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>
}
私が言ったように、私は元の画像から小さな画像を作成して保存するコードをコントローラーに追加するのに助けが必要です。助けてくれてありがとう