我正尝试在ASP MVC5项目中创建用户个人资料图片。我添加了一个名为changepicture.cshtml
的页面,并显示当前用户的图片。显示上传到服务器的图片
我有一个上传功能,将拍摄人像hank.jpg
上传的图片,并将其重命名为users_id.jpg
。
例如:
System.Data.Entity.DynamicProxies.ApplicationUser_9C8230B38B954135385F2B0311EAC02ED8B95C4D504F8424BA3ED79B37F0AAAF.jpg
我想通过抓住有用户ID和添加到显示每个用户在页面个体图像。 jpg,我该怎么做?
changepicture.cshtml
@model KidRoutineLogging.Models.ChangePictureViewModel
@{
ViewBag.Title = "Change Picture";
}
<h2>@ViewBag.Title.</h2>
<h4>Your Current Picture : @Html.ViewBag.CurrentPicture</h4>
<img src="@Url.Content("~/uploads/hank.jpg")" />
<br />
<br />
@using (Html.BeginForm("", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="FileUpload1" /><br />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
HomeController.cs
public async Task<ActionResult> Index()
{
foreach (string upload in Request.Files)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
//string filename = Path.GetFileName(Request.Files[upload].FileName);
//Request.Files[upload].SaveAs(Path.Combine(path, filename));
Request.Files[upload].SaveAs(Path.Combine(path, user + ".jpg"));
}
return View();
}
你的路径应该是'Request.Files [upload] .SaveAs(Path.Combine(path,user.Id +“.jpg”));'。我不是100%哪一部分是问题?显示图片或使用用户标识保存图片作为文件名的一部分? –
我的问题是显示这个上传的文件。
–
SpruceTips