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!
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!
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();
}
The image tag's src needs to point to where the image resides on a web server.
<img src="http://myserver.com/myimage.png" />
You need to reference images with relative address like this: ../images/1.png
.
check this tutorial to start: W3schools Html Tutorial/img Tag
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.