2011-06-16 101 views
1

我開發了一個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> 
+0

實際圖像存儲在哪裏?你說你只保存這個名字。 – Oded 2011-06-16 09:53:22

+0

目前顯示什麼? – foxy 2011-06-16 09:53:33

+0

嗨,感謝您給予答覆,存儲在服務器路徑圖像文件夾中的實際圖像。 – 2011-06-16 09:56:26

回答

0

使用圖像服務器路徑。它會像www.xyz.com/images.abc.png一樣,你應該給出像這樣的圖像路徑,以便它將從服務器獲取圖像路徑

2

只需使用路徑圖像中的img element

<img src="path to image" /> 
+0

我可以在我的本地機器上使用圖像路徑獲取圖像我得到的圖像,但在服務器上沒有顯示... – 2011-06-16 10:01:31

+0

@ user737497 - 服務器上的圖像? – Oded 2011-06-16 10:01:57

+0

是將圖像保存在服務器路徑中 – 2011-06-16 10:08:28

6

,你可以設置一個控制器的動作,這將成爲圖像:

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="" /> 
+0

嗨,謝謝你的迴應。在我的本地機器和服務器上(我的代碼)正常工作,但在客戶機上它不工作... – 2011-06-16 10:19:31

+0

@ user737497,客戶機是什麼?定義不工作?當你用FireBug調試請求時,你看到了什麼? – 2011-06-16 10:22:26

+0

實際上在我的數據庫中有兩個字段是postdata和第二個圖像name.i要顯示postdata和圖像,在本地機器和服務器工作正常,但在客戶端機器不工作它只顯示postdata。 – 2011-06-16 10:30:14

2

這是你的控制器..

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> 

希望這有助於...