显示存储在文件夹中的用户的个人资料图片

时间:2013-09-28 06:51:49

标签: asp.net-mvc image ravendb

我已将用户的个人资料图片上传到文件夹并将其路径保存在db.Here是我的上传代码

       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();
    }

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

User @Url.Content() and pass image path in parameter.

{
<img src="@Url.Content(Model.Path)" />
}

答案 1 :(得分:2)

如果您允许从模型中获取图像路径,则您也可以使用以下代码。

public ActionResult DisplayPic()
{
   var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), "heroAccent.png");
   return File(path, "image/jpeg");
}

<img src="@Url.Action("DisplayPic","FileManagement")" />

直接传递'FileManagement'作为控制器名称而不是'FileManagementController'。