Reputation: 33
I am really struggling from the below problem for long time. That would really helpful and appreciated if anyone could solve the problem!!
Environment:
Problem:
Sample Code
@{
Layout = null;
var imageName = "sampleImage.png";
var result = Url.Action("GetImage", new { imageName });
}
<div>
<img src="@result" alt="error" />
</div>
namespace Test.Controllers
{
public class TestController : Controller
{
private readonly IWebHostEnvironment _env;
public TestController(IWebHostEnvironment env)
{
this._env = env;
}
[HttpGet]
[Route("[action]")]
public IActionResult GetImage(string imageName)
{
string path = Path.Combine(this._env.WebRootPath,"images",imageName);
byte[] bytes = System.IO.File.ReadAllBytes(path);
return File(bytes, "image/png");
}
}
}
Upvotes: 0
Views: 107