当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - Display profile picture of a user which is stored in a folder

问题描述:

I have uploaded the profile picture of a user to a folder and saved its path in db.Here is my code for uploading

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

}

Now,i want to show the uploaded profile picture in a image control.

In my view i have a img control.

@using (Html.BeginForm())

 {

<img src="@Url.Action("UploadPic", "FileManagementController")" />

}

Code for displaying picture of a user who is using his account.I have get the path of the picture and returned a file based on that path.

How can i display that picture in the image control that is in the view.

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

}

Any help will be appreciated.

网友答案:
User @Url.Content() and pass image path in parameter.

{
<img src="@Url.Content(Model.Path)" />
}
网友答案:

youu can use below code also if you are enable to get image Path from Model.

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")" />

Pass just pass 'FileManagement' as controller name instead of 'FileManagementController'.

分享给朋友:
您可能感兴趣的文章:
随机阅读: