如何在asp.net中显示图像mvc3
我开发了一个asp.mvc3 web应用程序,在这里我将图像名称和一些文本保存到数据库中。当我尝试从数据库中获取图像时,它不显示image.Actually在我的本地机器上,每件事情都很好,但是当我在服务器上测试时,它不起作用,请helpme。如何在asp.net中显示图像mvc3
<% for (int v = 0; v < Listdata.Count; v++)
{ %>
<%if (j == 1)
{ %>
<%if (count < Listdata.Count)
{ %>
<tr>
<%string Imageurl = Listdata[count].ToString();%>
<%string[] GetImages = Imageurl.Split(','); %>
<%string imagedata = GetImages[1].ToString(); %>
<% Getimage1 = imagedata.Substring(9, imagedata.Length - 10); %>
<li class="menu"><a href='<%=Html.Encode(Geturl) %>'>
<img src='/Images/<%=Html.Encode(Getimage1)%>' alt="" style="margin-top: 0px; width: auto;
height: 200px;" /></a></li>
<%} %>
<td>
<li class="menu"><span style="float: left; margin-left: 5px;">
<%=Html.Encode(Postdate)%></span><br />
<a href="DisplayData/<%=Html.Encode(item.postid) %>"><span class="name">
<%=Html.Encode(item.post)%></span><span class="arrow"></span></a></li>
<%j++; i = 2; count++; %>
<%}
}
} %>
<%} %>
</td>
使用图像服务器路径。它会像www.xyz.com/images.abc.png一样,你应该给出像这样的图像路径,以便它将从服务器获取图像路径
只需使用路径图像中的img
element:
<img src="path to image" />
我可以在我的本地机器上使用图像路径获取图像我得到的图像,但在服务器上没有显示... – 2011-06-16 10:01:31
@ user737497 - 服务器上的图像? – Oded 2011-06-16 10:01:57
是将图像保存在服务器路径中 – 2011-06-16 10:08:28
,你可以设置一个控制器的动作,这将成为图像:
public ActionResult MyImage()
{
byte[] image = ... go and fetch from DB
return File(image, "image/png");
}
,并在你看来简单地引用此动作:
<img src="<%= Url.Action("MyImage", "SomeController") %>" alt="my image" />
注意如何使用Url.Action
帮助程序构建映像的url,而不是硬编码,这将确保此代码无论在何处托管应用程序都能正常工作。
在你的代码已经硬编码这当然不会,因为当你因为需要使用IIS中的虚拟目录的名称部署应用程序工作的网址:
<img src='/Images/<%=Html.Encode(Getimage1)%>' alt="" />
所以使用URL打交道时总是使用URL助手:
<img src="<%= Url.Action("Getimage1", "Images") %>" alt="" />
嗨,谢谢你的回应。在我的本地机器和服务器上(我的代码)正常工作,但在客户机上它不工作... – 2011-06-16 10:19:31
@ user737497,客户机是什么?定义不工作?当你用FireBug调试请求时,你看到了什么? – 2011-06-16 10:22:26
实际上在我的数据库中有两个字段是postdata和第二个图像name.i要显示postdata和图像,在本地机器和服务器工作正常,但在客户端机器不工作它只显示postdata。 – 2011-06-16 10:30:14
这是你的控制器..
public ActionResult LoadImg(int id)
{
byte[] image = null;
tblImage img = en.tblImages.FirstOrDefault(i => i.ImgId == id);
image = (byte[])img.ImgSrc;
return File(image, "image/png");
}
这是你的看法
<div id="imagebox1" align="center">
<img height="80" width="140" class="imgThumb" src="@Url.Action("LoadImg", "Image", new { id = m.ImgId })" alt="Loading..." />
</div>
希望这有助于...
实际图像存储在哪里?你说你只保存这个名字。 – Oded 2011-06-16 09:53:22
目前显示什么? – foxy 2011-06-16 09:53:33
嗨,感谢您给予答复,存储在服务器路径图像文件夹中的实际图像。 – 2011-06-16 09:56:26