hi i am doing a project in MVC4 using c#. in my project i am storing some group of persons details in database with unique id.and also storing the profile pic of particular person in a folder in my project.(Id number and pic name is same). in view i want display all these details.Problem is in images. I use the following codes
View
@foreach(var item in Model)
{
<td><img src="@Url.Action("ImageRetrive", "Member", new {imgname=(item.Id)})" /><br />Rtn. @item.Mem_NA<br />(@item.Mem_Occ)</td>
}
controller
public string ImageRetrive(int imgname)
{
string keyword=image.ToString();
string imagefolderpath = Server.MapPath("~/Content/Member/MemberPhotos");
string currentimage = new Member().GetImage(imagefolderpath,keyword);
string fullpath = "~/Content/Member/MemberPhotos/" + currentimage;
return fullpath;
}
model
public string GetImage(string path,string keyword)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] images = di.GetFiles();
foreach (FileInfo image in images)
{
var name = image.Name;
if (name.Contains(keyword))
{
imgname = name;
}
}
return imgname;
}
But i didnot get any output. in the controller the variable fullpath is giving the link. but it can take in . Please help me.
I think you need is FilePathResult
, change your controller code as
public FilePathResult ImageRetrive(int imgname)
{
string keyword=image.ToString();
string imagefolderpath = Server.MapPath("~/Content/Member/MemberPhotos");
string currentimage = new Member().GetImage(imagefolderpath,keyword);
string fullpath = "~/Content/Member/MemberPhotos/" + currentimage;
return File(fullpath, "image/png"); //Changed here
}
or You can use FileContentResult
public FileContentResult Retrive(int imgname)
{
return File(ConvertToByteArray(YourFile), "image/png"); //Changed here
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments