我试图在视图页面中显示图像,但它不显示在浏览器中。 这是我的代码: MyChat.cshtml使用Url.Action在视图页面中不显示图像
<td>
<img src="@Url.Action("GetImage", "Mailbox", new { name = Session["Loginname"].ToString() })" id="photo"/>
</td>
控制器
public FileContentResult GetImage(string name)
{
byte[] cover = GetImageFromDataBase(name);
ViewData["image"] = cover;
if (cover != null)
{
return File(cover, "image/jpeg");
}
else
{
return null;
}
}
public byte[] GetImageFromDataBase(string name)
{
RegisterLogic logic = new RegisterLogic();
byte[] image = logic.GetImage(name);
return image;
}
我用于从SQL服务器2008 R2检索值的业务逻辑是:
public byte[] GetImage(string name)
{
con.ConnectionString = str;
cmd.Connection = con;
if (con.State == ConnectionState.Open)
con.Close();
if (con.State == ConnectionState.Closed)
con.Open();
cmd.CommandText = "Select Picture from InspiniaMessage where UserName='" + name + "'";
cmd.CommandType = CommandType.Text;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
Message m = new Message();
foreach (DataRow dr in dt.Rows)
{
Message mn = new Message();
mn.Picture = (byte[])(dr["Picture"]);
m.Picture = mn.Picture;
}
return m.Picture;
con.Close();
}
}
我没有任何错误,但图像不显示在视图中。
看到这一点: http://stackoverflow.com/questions/29576699/how-to-return-url-string-from-controller- a-controller-in-mvc-4/29577189#29577189 –