1

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>
11
  • Where is the actual image stored? You say you save the name only. Commented Jun 16, 2011 at 9:53
  • What is it currently showing? Commented Jun 16, 2011 at 9:53
  • Hi thank you for giving responce, actual image stored in server path images folder. Commented Jun 16, 2011 at 9:56
  • currently it showing only text data in server,but in my local machine images are displayed . Commented Jun 16, 2011 at 9:57
  • What code do you have at the min? Can you show us what you have done Commented Jun 16, 2011 at 10:02

4 Answers 4

6

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="" />
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thank you for ur response.In my local machine and server it is(My code) working but on client machine it not working ...
@user737497, what client machine? Define not working? When you debug the requests with FireBug what do you see?
actually in my data base two fields are ther that is on for postdata & 2nd for image name.i want display the postdata and image, in local machine and server working fine,but in client machine not working it display only postdata .
2

Just use the path to the image in an img element:

<img src="path to image" />

3 Comments

i can get images in my local machine using image path i get images, but in server not showing ...
@user737497 - Are the images on the server?
Can you access the image directly on the server, using this path?
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.