0

How to display this image in an ASP.NET MVC project?

I've tried this, but it wouldn't work:

<img src="C:\BBBB\1.png" />

Any brilliant suggestion, please?

Thanks a lot!

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Lucie kulza
  • 1,357
  • 6
  • 20
  • 31

4 Answers4

1

I did something similar to this to display payroll stubs. I didn't want them accessible from within wwwroot.

See this article for serving an image from an mvc action.

ASP.NET MVC3: Image loading through controller

public ActionResult ShowImage(id) 
{
    var file = @"C:\BBB\"+id;
    var ext = file.Split('.').Last();
    return File(file, "image/"+ext, Path.GetFileName(file));
}

Call it like this:

<img src="@Url.Action("ShowImage",new { id="1.png" })" alt="1.png"/>

For a loop, you could do something like this in your existing view:

 for (var i = 1; i < 10; i++ ) {
    @Html.Partial("ShowIfExists", new {id=i.ToString()+".png" })
 }

Create a new Razor View called _ShowIfExists.cshtml

 <img src="@Url.Action("ShowImage",new { id=Model })" alt="1.png"/>

And add a corresponding action:

public ActionResult ShowIfExists(id)
    if ( System.IO.File.Exists(@"C:\BBB\"+id) )
        return PartialView("_ShowIfExists",null,id);
    }
    return new EmptyResult();
}
Community
  • 1
  • 1
B2K
  • 2,541
  • 1
  • 22
  • 34
0

The image tag's src needs to point to where the image resides on a web server.

<img src="http://myserver.com/myimage.png" />
Adam Zuckerman
  • 1,633
  • 1
  • 14
  • 20
0

You need to reference images with relative address like this: ../images/1.png.

check this tutorial to start: W3schools Html Tutorial/img Tag

Reza Owliaei
  • 3,293
  • 7
  • 35
  • 55
0

If you insist on displaying a local image, you should include the protocol, which in this case is file://:

<img src="file://C:/BBBB/1.png" />

This is not a good idea for real projects tho.

nxu
  • 2,202
  • 1
  • 22
  • 34