我已将用户的个人资料图片上传到文件夹并将其路径保存在数据库中。这是我的上传代码
public ActionResult UploadPic(FileManagement fmanage, HttpPostedFileBase file)
{
string email = User.Identity.Name;
if (file != null && file.ContentLength > 0)
{
var FileName = string.Format("{0}.{1}", Guid.NewGuid(), Path.GetFileName(file.FileName));
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), FileName);
file.SaveAs(path);
using (var session = DocumentStore.OpenSession("RavenMemberShip"))
{
var query = from q in Session.Query<Registration>() where q.Email == email select q;
if (query.Count() > 0)
{
foreach (var updated in query)
{
updated.FileName = FileName;
updated.Path = path;
session.SaveChanges();
}
}
}
}
else ModelState.AddModelError("", "Remove the errors and try again");
return View();
}
现在,我想在图像控件中显示上传的个人资料图片。在我看来,我有一个 img 控件。@using (Html.BeginForm())
{
<img src="@Url.Action("UploadPic", "FileManagementController")" />
}
用于显示正在使用其帐户的用户的图片的代码。我已经获取了图片的路径并根据该路径返回了一个文件。如何在视图中的图像控件中显示该图片。
public ActionResult DisplayPic(FileManagement fm)
{
string ipath;
string UserName = User.Identity.Name;
var getPath = from p in Session.Query<Registration>()
where p.Email == UserName
select p;
if (getPath.Count() > 0)
{
foreach (var imgpath in getPath)
{
ipath = imgpath.Path;
return base.File(ipath, "image/jpg");
}
}
return View();
}
任何帮助将不胜感激。