Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc load image src converty path

Tags:

asp.net-mvc

How to load image file from physical path with out create virtual directory? I use C# code behaind and image source is physical path? How tio convert C:\Folder\imageName.jpg to file:///C:/Folder/imageName.jpg

like image 884
Ivan Avatar asked Feb 13 '26 09:02

Ivan


1 Answers

You need to use a controller action to serve that image:

public ActionResult MyImage()
{
    return File(@"C:\Folder\imageName.jpg", "image/jpg");
}

and in your view invoke this controller action to show the image:

<img src="@Url.Action("MyImage", "SomeController")" alt="myimage" />

The reason for this is because client browsers cannot access arbitrary files located on the server. If this image is not inside the virtual directory it cannot be referenced by a client. So it is the server that needs to expose it.

like image 58
Darin Dimitrov Avatar answered Feb 16 '26 05:02

Darin Dimitrov