C# MVC Load Image Asychrnously
Razor HTML Code
<img width="130" class="img-bordered" src="@Url.Action("RenderImage", new { path= "yourpath" })" height="100" alt="NONE" />
C# Code Action
public async Task<ActionResult> RenderImage(string path)
{
string path = Server.MapPath(path);
if (System.IO.File.Exists(path))
{
var b = System.IO.File.ReadAllBytes(path);
return File(b, "image/jpeg");
}
else
{
string path1 = Server.MapPath("~/noimage.png");
var b = System.IO.File.ReadAllBytes(path1);
return File(b, "image/png");
}
}
<img width="130" class="img-bordered" src="@Url.Action("RenderImage", new { path= "yourpath" })" height="100" alt="NONE" />
C# Code Action
public async Task<ActionResult> RenderImage(string path)
{
string path = Server.MapPath(path);
if (System.IO.File.Exists(path))
{
var b = System.IO.File.ReadAllBytes(path);
return File(b, "image/jpeg");
}
else
{
string path1 = Server.MapPath("~/noimage.png");
var b = System.IO.File.ReadAllBytes(path1);
return File(b, "image/png");
}
}
Comments
Post a Comment