我試圖在視圖頁面中顯示圖像,但它不顯示在瀏覽器中。 這是我的代碼: 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 –