简体   繁体   中英

How to display image in asp.net mvc3

I have develop a asp.mvc3 web application in this i have save the image name and some text into database.When i try to get the image from data base it doesn't show image.Actually in my local machine every thing is working fine but when i test in server it's not working please helpme.

 <% 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>

You could setup a controller action which will serve the image:

public ActionResult MyImage()
{
    byte[] image = ... go and fetch from DB
    return File(image, "image/png");
}

and in your view simply reference this action:

<img src="<%= Url.Action("MyImage", "SomeController") %>" alt="my image" />

Notice how the url to the image is built using the Url.Action helper instead of it being hardcoded which would ensure that this code would work no matter where the application is hosted.

In your code you have hardcoded the url which of course won't work because when you deploy your application because there is the name of the virtual directory in IIS that needs to be used:

<img src='/Images/<%=Html.Encode(Getimage1)%>' alt="" />

So always use URL helpers when dealing with urls:

<img src="<%= Url.Action("Getimage1", "Images") %>" alt="" />

只需在img元素中使用图像的路径即可:

<img src="path to image" />

Here is your controller..

 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");
    }

Here is your view

<div id="imagebox1" align="center">
                <img height="80" width="140" class="imgThumb" src="@Url.Action("LoadImg", "Image", new { id = m.ImgId })" alt="Loading..." />
            </div>

Hope this helps...

Use image server path. it will be like www.xyz.com/images.abc.png, you should give image path like this, so that it will take image path from server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM